in reply to better way to find list members?

print "I am here!" if grep {/$bar/} @foo;

Update and disclaimer: Yes, all the points below are correct, this was just a quick one-liner. Don't use it for things that might actually Take Time or Need To Be Efficient. Use one of the methods above.

On second thought, why should I be that lazy? Here's a rewrite to specs:

sub IsItThere($$) { my $testarray = shift; my $string = shift; for (@$testarray) { return 1 if $_ eq $string; ## Or... return 1 if /$string/i; etc. etc. } return 0; }

Replies are listed 'Best First'.
RE: RE: better way to find list members?
by japhy (Canon) on Oct 17, 2000 at 00:36 UTC
    No no no no no no no. The FAQ strongly suggests against this, and for many reasons:
    • grep() does not short circuit -- once it finds a match, it will keep going through the rest of the list
    • the regex you use is compiled EVERY SINGLE TIME -- this can be slow and irritating
    • the regex you use will say ('foo','bar','blat') contains the element 'ar', even though it doesn't
    • you shouldn't even correct the regex (that is, add the \A and \z anchors, and \Q and \E escapes), you should use $_ eq $bar
    • DON'T USE GREP


    $_="goto+F.print+chop;\n=yhpaj";F1:eval