Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Sparing multiple 'or's

by Eily (Monsignor)
on Jun 04, 2018 at 15:24 UTC ( [id://1215843]=note: print w/replies, xml ) Need Help??


in reply to Sparing multiple 'or's

You can either use grep like this: if (grep {$variable eq $_} qw( ABA SCO ACC PHC GHF )) (where qw builds a list of words).
Or use List::Util's any (with its more intuitive name, and slightly faster since it will stop searching as soon as a match is found):

use List::Util qw( any ); if (any {$variable eq $_} qw( ABA SCO ACC PHC GHF ))

$a is a special variable, than can be used by sort or some functions of List::Util, that's why I used $variable instead.

And also note that I used eq, which compares two strings, while == compares numbers. Perl will actually complain about this if you have warnings enabled (which you really should, as it lets perl tell you where it thinks you have done a mistake, and it is often right).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-25 14:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found