Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

beginner scripting question re: if elsif else

by jhumphreys (Novice)
on Oct 11, 2012 at 20:11 UTC ( #998530=perlquestion: print w/ replies, xml ) Need Help??
jhumphreys has asked for the wisdom of the Perl Monks concerning the following question:

Knowledgeable Monks-

The following if elsif else script should stop and print the associated message if "snowing" is inputted in answer, but it doesn't. Please advise. Thanks, as always, Monks. You guys rock

#!/usr/bin/perl # walkies.pl use warnings; use strict; print "What's the weather like outside? "; my $weather = <STDIN>; print "How hot is it, in degrees Celsius? "; my $temperature = <STDIN>; print "And how many emails left to reply to? "; my $work = <STDIN>; chomp($weather, $temperature); if ($weather eq "snowing") { print "OK, let's go!\n"; } elsif ($weather eq "raining") { print "No way, sorry, I'm staying in.\n"; } elsif ($temperature < 18) { print "Too cold for me!\n"; } elsif ($work > 30) { print "Sorry - just too busy.\n"; } else { print "Well, why not?\n"; }

Comment on beginner scripting question re: if elsif else
Download Code
Re: beginner scripting question re: if elsif else
by kennethk (Monsignor) on Oct 11, 2012 at 20:21 UTC
    My guess is that you mean you want execution to stop before your get to question #2. In order for that to hold, you need to test the value of $weather before your execute your next print. Perhaps you mean something more like:
    #!/usr/bin/perl # walkies.pl use warnings; use strict; print "What's the weather like outside? "; chomp(my $weather = <STDIN>); if ($weather eq "snowing") { print "OK, let's go!\n"; exit; } elsif ($weather eq "raining") { print "No way, sorry, I'm staying in.\n"; exit; } print "How hot is it, in degrees Celsius? "; my $temperature = <STDIN>; if ($temperature < 18) { print "Too cold for me!\n"; exit; } print "And how many emails left to reply to? "; my $work = <STDIN>; if ($work > 30) { print "Sorry - just too busy.\n"; } else { print "Well, why not?\n"; }
    If you want to avoid explicit exits, you could do the same thing with nested if-elsif-else's:
    #!/usr/bin/perl # walkies.pl use warnings; use strict; print "What's the weather like outside? "; chomp(my $weather = <STDIN>); if ($weather eq "snowing") { print "OK, let's go!\n"; } elsif ($weather eq "raining") { print "No way, sorry, I'm staying in.\n"; } else { print "How hot is it, in degrees Celsius? "; my $temperature = <STDIN>; if ($temperature < 18) { print "Too cold for me!\n"; } else { print "And how many emails left to reply to? "; my $work = <STDIN>; if ($work > 30) { print "Sorry - just too busy.\n"; } else { print "Well, why not?\n"; } } }

    There are a multitude of other ways to do this, but it all comes down to thinking about the logical progression of what you want to accomplish.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      kennethk-

      Thanks for your reply. So, with if elsif else, if the first condition is true, the whole script still runs anyway (if no exits scripted)?

      j.

        Yes, the first part of your code gathers information from the user, then the second part actually does something with it. The way you have it, it will ask all the questions, and get all the answers from the user BEFORE getting to the if/else section. Once it is in the if/else section, it will do as you suspect -- i.e. if it is snowing, then that satisfies the if condition and therefore the else conditions are skipped.

Re: beginner scripting question re: if elsif else
by runrig (Abbot) on Oct 11, 2012 at 20:46 UTC
    The input includes the newline character at the end of the string. You either need to remove the newline or your 'eq' needs to account for it.
Re: beginner scripting question re: if elsif else
by BillKSmith (Chaplain) on Oct 12, 2012 at 03:06 UTC
    You must remove the newline from each input before you test it. Use the function chomp.
    Bill

      Just a note, jhumphreys did chomp the input in the original program:

      chomp($weather, $temperature);

      Not the syntax I would use (would be more idiomatic and chomp at input as some of the posters did), but nonetheless, the newlines should be gone.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2014-12-27 21:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls