Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Improving if statements

by choroba (Cardinal)
on Sep 18, 2014 at 23:40 UTC ( [id://1101123]=note: print w/replies, xml ) Need Help??


in reply to Re: Improving if statements
in thread Improving if statements

Hash, unlike if, has no order. If the order matters, you might need something like the following (alternatives in regexes match from the leftmost one):
my @keys = ('test', 'dark', 'white', 'house', 'all things', 'money', 'count +ry'); my @values = qw(blue black light home multi value USA); my $regex = join '|', @keys; # "map quotemeta" might be needed for ugl +ier keys $regex = qr/($regex)/; my %switch; @switch{@keys} = @values; my $change; if ($item =~ $regex) { $change = $switch{$1}; } else { $change = "neutral"; } print "Final Item = $change.\n";
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^3: Improving if statements
by Anonymous Monk on Sep 18, 2014 at 23:55 UTC
    Order will not matter, I am more concerned about speed.

Log In?
Username:
Password:

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

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

    No recent polls found