Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Simple IF, IF, IF or ELSE

by abachus (Monk)
on Jun 28, 2006 at 20:44 UTC ( #558144=perlquestion: print w/replies, xml ) Need Help??

abachus has asked for the wisdom of the Perl Monks concerning the following question:

Hello once again !

The code below as you can see is a simple way to assign a value to $drink after $punter has been evaluated.

if($punter eq 'coffee'){ $drink = 'non alky'; } if($punter eq 'beer'){ $drink = 'alky'; } if($punter eq 'whisky'){ $drink = 'proper alky'; } else { $drink = 'unknown'; }

Now, surely there is a better way to do it, by that i mean with less lines of code, and if possible doing the same in a more efficient way.

I look forward to any answers, thanks,

Isaac.

Replies are listed 'Best First'.
Re: Simple IF, IF, IF or ELSE
by ikegami (Patriarch) on Jun 28, 2006 at 20:48 UTC
    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'; }
      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';
Re: Simple IF, IF, IF or ELSE
by GrandFather (Saint) on Jun 28, 2006 at 21:02 UTC

    Fir a start that probably doesn't do what you mean. Consider:

    use warnings; use strict; my $drink; my $punter = 'coffee'; if($punter eq 'coffee'){ $drink = 'non alky'; } if($punter eq 'beer'){ $drink = 'alky'; } if($punter eq 'whisky'){ $drink = 'proper alky'; } else { $drink = 'unknown'; } print $drink;

    Prints:

    unknown

    Better would be:

    use warnings; use strict; my $drink; my $punter = 'coffee'; if ('coffee' eq $punter){ $drink = 'non alky'; } elsif ('beer' eq $punter){ $drink = 'alky'; } elsif ('whisky' eq $punter) { $drink = 'proper alky'; } else { $drink = 'unknown'; } print $drink;

    which prints:

    non alky

    as expected. However probably better still would be:

    use warnings; use strict; my %drinkType = ('coffee' => 'non alky', 'beer' => 'alky', 'whisky' => + 'proper alky'); my $drink; my $punter = 'coffee'; if (exists $drinkType{$punter}) { $drink = $drinkType{$punter}; } else { $drink = 'unknown'; } print $drink;

    If you have more work to do for each if clause you could:

    use warnings; use strict; my %drinkType = ('coffee' => \&doNonalky, 'beer' => \&doAlky, 'whisky' + => \&doProperAlky); my $drink; my $punter = 'coffee'; if (exists $drinkType{$punter}) { $drinkType{$punter}->(); } else { print 'unknown'; } sub doNonalky { print 'non alky'; } sub doAlky { print 'alky'; } sub doProperAlky { print 'proper alky'; }

    DWIM is Perl's answer to Gödel

      Indeed my example fails, i don't understand why, (sure i now know how to fix it, but i still don't get it) though that is for me to go read and this time try to understand the chapter again. I think it was like chapter one or something ;)

      It does appear that the hash method is clearly advantageous in at atleast two ways i can think of, arbitrary length and refs/sub-refs. without needing a long list of if,elsif,else.

      I thank you folks, i've some things to go away and consider.

      Isaac.

        The else only applies to the most recent if. In your code you had a number of solitary if statements, then a if/else statement.

        You should read through the tutorials sections here, especially Flow Control Structures, but the whole Getting Started with Perl section would stand you in good stead. Without meaning to be unkind: you have some major gaps in your understanding of Perl (and programming languages generally) at this point!


        DWIM is Perl's answer to Gödel
Re: Simple IF, IF, IF or ELSE
by VSarkiss (Monsignor) on Jun 28, 2006 at 20:58 UTC

      but note that using the switch module is frowned on. It's (by repute) a little buggy. The other sugestions in the link provided are generally to be prefered.


      DWIM is Perl's answer to Gödel
Re: Simple IF, IF, IF or ELSE
by traveler (Parson) on Jun 28, 2006 at 21:49 UTC
    And just to emphazise TIMTOWTDI:
    $drink = 'unknown'; $drink = 'non alky' if $punter eq 'coffee'; $drink = 'alky' if $punter eq 'beer'; $drink = 'proper alky' if $punter eq 'whisky'
    or (kind of ugly)
    $drink = $punter eq 'coffee'?'non alky':$punter eq 'beer'?'alky':$punt +er eq 'whisky'?'proper alky':'unknown';

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2022-08-12 06:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?