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

Re: Comparing against multiple values (boo)

by boo_radley (Parson)
on Mar 18, 2002 at 19:36 UTC ( [id://152573]=note: print w/replies, xml ) Need Help??


in reply to Comparing against multiple values

While that's very easy, it's not as fast as using a comparison operator such as 'eq' or '=='. However using these often leads to redundant and cumbersome looking code:
have you met my friend, grep?

my @values= qw(foo bar baz bax); my $test = "foo"; print "woo\n" if (grep {$_ eq $test}@values) ;
This is probably not the hottest use for grep if you've got a long list of values, since it'll build an array of values matching the test only to use the result in a scalar fashion. It'd be handy if you could last in a grep in this particular instance.
You could produce the same behavior in a for loop quite easily that would exit on the first found value.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2024-04-25 11:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found