Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Simple IF, IF, IF or ELSE

by GrandFather (Saint)
on Jun 28, 2006 at 21:02 UTC ( [id://558150]=note: print w/replies, xml ) Need Help??


in reply to Simple IF, IF, IF or ELSE

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

Replies are listed 'Best First'.
Re^2: Simple IF, IF, IF or ELSE
by abachus (Monk) on Jun 28, 2006 at 21:25 UTC

    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

        I'm not offended GrandFather, i'm a self taught idiot, hehe :) I'm here to benefit from others wisdom, and when the time is right (and i'm able) i'd enjoy passing that wisdom onwards.

        I had to dash off to test a pint of beer in the pub, and after giving some careful thought as to why IF, IF, IF, ELSE would not work in the way i first put the code across, I came up with :

        By using an IF statement, followed further IF statements brings AND into the equation, no ?

        if(true){ do this } if(true){ do that } else{ do something else }

        The first two IF's will return true and not 'do else' only if both IF's return true, maybe i'm wrong here, don't know, not yet have i read the suggested documentation, but i will do so shortly. No doubt there i will find my answer.

        For too long i've used 'do stuff if(true)' and thats not the same thing, as i now know.

        thanks for your patience,

        Isaac.

Log In?
Username:
Password:

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

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

    No recent polls found