Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: boolean IN(); function in perl like

by Anonymous Monk
on Jan 14, 2005 at 19:16 UTC ( [id://422349]=note: print w/replies, xml ) Need Help??


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.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://422349]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-18 07:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found