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

Re^2: using if-elsif-else

by chargrill (Parson)
on Dec 29, 2006 at 04:43 UTC ( #592168=note: print w/replies, xml ) Need Help??


in reply to Re: using if-elsif-else
in thread using if-elsif-else

I was operating under the assumption that a user could enter "Kevin Johnson" and still get the value for $phnums{'kevin'} - i.e. more than what might be used as the explicit hash key. Possibly a poor assumption, but I couldn't think of a better reason for using regexen in the OP's code :-)

I recently ran across a hash setup as a dispatch table where most values were well defined, but some could match a variety - these were handled (in a generalized manner) like so:

if( exists $hash{$key} ){ &$hash{$key}; } elsif( $key =~ /phrase/ ){ # several could match but all required the same action &$hash{'some_action'}; } else { &$hash{'default_action'}; }


--chargrill
s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)

Replies are listed 'Best First'.
Re^3: using if-elsif-else
by thevoid (Scribe) on Dec 29, 2006 at 10:39 UTC
    I was operating under the assumption that a user could enter "Kevin Johnson" and still get the value for $phnums{'kevin'} - i.e. more than what might be used as the explicit hash key.

    You're right, that was the idea :-) Thanks for the alternative code.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2019-10-20 08:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?