Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Flag variables

by blakem (Monsignor)
on Mar 03, 2003 at 10:58 UTC ( #239997=note: print w/ replies, xml ) Need Help??


in reply to Flag variables

How about:

if (grep {$_ eq "foo"} @somearray) { someaction(); }
Or even:
someaction() if grep {$_ eq "foo"} @somearray;

-Blake


Comment on Re: Flag variables
Select or Download Code
Re: Re: Flag variables
by jsegal (Friar) on Mar 03, 2003 at 15:34 UTC
    Though it is always good to use built-ins like "grep" whenever possible, in this case there is a difference between this code and the original: In the original, once the test succeeds, it is no longer performed (i.e. last is called), whereas here the test will be performed on every item in the list. If this is a simple equality test, that is not so bad, but if it is something like a database lookup, and the list is long, that is a lot of extra overhead.

    Now if grep were smart enough to shortcut when called in "boolean" context, then there would be no reason not to use it. Of course, "boolean" context per se won't exist until perl 6...

    --JAS
      Now if grep were smart enough to shortcut when called in "boolean" context, then there would be no reason not to use it. Of course, "boolean" context per se won't exist until perl 6...

      Until then, using first from List::Util is one alternative.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2014-12-27 20:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls