Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Simple IF, IF, IF or ELSE

by ikegami (Patriarch)
on Jun 28, 2006 at 20:48 UTC ( [id://558145]=note: print w/replies, xml ) Need Help??


in reply to Simple IF, IF, IF or ELSE

That doesn't quite work. Try it for coffee, and you get 'unknown'. Fix:
if ($punter eq 'coffee') { $drink = 'non alky'; } elsif ($punter eq 'beer' ) { $drink = 'alky'; } elsif ($punter eq 'whisky') { $drink = 'proper alky'; } else { $drink = 'unknown'; }
Alternative:
my %lookup = ( 'coffee' => 'non alky', 'beer' => 'alky', 'whisky' => 'proper alky', ); if (exists $lookup{$punter}) { $drink = $lookup{$punter}; } else { $drink = 'unknown'; }

Replies are listed 'Best First'.
Re^2: Simple IF, IF, IF or ELSE
by dsheroh (Monsignor) on Jun 28, 2006 at 22:02 UTC
    Just to compress your alternative method a little bit more:
    my %lookup = ( 'coffee' => 'non alky', 'beer' => 'alky', 'whisky' => 'proper alky', ); $drink = exists $lookup{$punter} ? $lookup{$punter} : 'unknown'; # or another option: # $drink = $lookup{$punter} || 'unknown';

Log In?
Username:
Password:

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

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

    No recent polls found