in reply to boolean IN(); function in perl like

grep

my @found = grep { $e eq $_ } @a; #or print "yes" if grep { $e eq $_ } @a;

Replies are listed 'Best First'.
Re^2: boolean IN(); function in perl like
by xdg (Monsignor) on Jan 14, 2005 at 21:40 UTC

    I'm surprised to see only grep examples with "eq". You can also do this with regex interpolation (being careful to match the whole thing, of course):

    print "found!" if grep(/^$e$/, @a);

    This might be useful if, for example, you didn't care about matching case:

    print "found!" if grep(/^$e$/i, @a);

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      You'd better run $e through quotemeta if you do this, or use the equivalent \Q .. \E re syntax.

      (If case does matter, an eq check is definitely going to be faster. If it doesn't, precomputing lc $e and comparing that to lc $_ is still liable to be faster, though it's worth a benchmark. Of course, very often this is not a concern.)