It really depends. Suppose you're looking for 'a b' as a value within the array. You would get a false positive if you use this method. To correct it, I guess you can do something along these lines:
my @array = qw( a b c d x ); my $sought = 'a'; $" = '|-nothing we expect-|'; print "Found!" if index($" . "@array" . $", $" . $sought . $") > -1;
Look at that disgusting code... Please note that the preferred way to do this is grep in a scalar context:
my @array = qw( a b c d x ); my $sought = 'a'; print "Found!" if grep $_ eq $sought, @array;
With this method, you only iterate over the array instead of all the characters within the array plus $". Since grep is used in a scalar context, it returns the number of matches within the array.
Update: Ah geez.. walk away to grab a drink, come back to finish your answer and two people have posted. You guys are quick :P
antirice
The first rule of Perl club is - use Perl
The ith rule of Perl club is - follow rule i - 1 for i > 1
In reply to Re: Is X in my array?
by antirice
in thread Is X in my array?
by Not_a_Number
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |