Re: how to test if an element belongs to an array or hash
- From: Brian McCauley <nobull@xxxxxxxx>
- Date: Tue, 31 May 2005 18:11:01 +0100
biomahui@xxxxxxxxx wrote:
If I don't use iteration, how to test if an element belongs to an array or hash?
Although there is a possible interpretation to the above question I very much doubt that it is really the question you really inteded to ask.
Please define the terms "an element" and "belongs an array or hash".
You can find out if a _string_ exists as a _key_ in a hash using exists($my_hash{$the_string})
There is no direct way to see if a string appears in an array or in the values of a hash - you need to use a loop or build a(nother) hash (for details see FAQ).
Given a scalar variable it is only possbile to find out if that scalar is part of some known array or hash using a loop (a bit like the one in the FAQ but with a couple of backslashes).
To answer the question you actually asked...
There is (to all normal intents and purposes) no way given a scalar variable to know if that scalar is an value in some unknown array or hash.
my $foo = 1; my @bar = (2);
for my $element ( $foo, @bar ) {
print "The value is $element and it is ";
print "not " unless some_impossible_function($element);
print "an array element\n";
}If some_impossible_function() could be written this would say:
The value is 1 and it is not an array element The value is 2 and it is an array element
.
- References:
- how to test if an element belongs to an array or hash
- From: biomahui
- how to test if an element belongs to an array or hash
- Prev by Date: Re: how to test if an element belongs to an array or hash
- Next by Date: Perl on a mainframe system
- Previous by thread: Re: how to test if an element belongs to an array or hash
- Next by thread: Perl on a mainframe system
- Index(es):
Relevant Pages
|
|