Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Sparing multiple 'or's

by dave_the_m (Monsignor)
on Jun 04, 2018 at 15:26 UTC ( #1215848=note: print w/replies, xml ) Need Help??


in reply to Sparing multiple 'or's

I'm presuming you intended string rather than numeric comparisons (so $a eq 'ABA'). In which case this does it:
if ($a =~ /^ ( ABA | SCO | ACC | PHC | GHF ) $/x ) { ... }

Dave.

Replies are listed 'Best First'.
Re^2: Sparing multiple 'or's
by jimpudar (Pilgrim) on Jun 04, 2018 at 18:47 UTC

    This is how I would do it, but I generally use a non-capturing group whenever possible for clarity:

    if ( $a =~ /^ (?: ABA | SCO | ACC | PHC | GHF ) $/x ) { ... }

    Note that this is more for capturing intent than performance. When I see a capture group (...), I assume we will be using whatever was captured. In this case, since we are capturing the entire string, $a == $1.

    Probably not worth the nitpick :)

    Best,

    Jim

    πάντων χρημάτων μέτρον έστιν άνθρωπος.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (10)
As of 2019-06-25 12:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Is there a future for codeless software?



    Results (106 votes). Check out past polls.

    Notices?