Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Conditional Elimination

by RichardK (Parson)
on Aug 30, 2011 at 11:56 UTC ( [id://923194]=note: print w/replies, xml ) Need Help??


in reply to Conditional Elimination

If you have a long list of items to check $p against, you could try something like

my @ptests = (88,89,90,91,110,115); if ( grep {$p == $_} @ptests ) { return $p; }

Then it's easy to add new items to the list and the code does not need to change.

or you could use List::MoreUtils firstval which may be slightly more efficient if you have long lists

use List::MoreUtils qw {firstval}; my @ptests = (88,89,90,91,110,115); if ( firstval {$p == $_} @ptests) { return $p; }

Replies are listed 'Best First'.
Re^2: Conditional Elimination
by Kc12349 (Monk) on Aug 31, 2011 at 22:19 UTC

    If you have 5.10 or newer perl, I would use the smart match operator using the same logic. Smart match generally benchmarks significantly faster than grep and first.

Log In?
Username:
Password:

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

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

    No recent polls found