Tech77 has asked for the wisdom of the Perl Monks concerning the following question:
I'm puzzled as to why not declaring my $guess; as opposed to using my $code = <STDIN>; didn't really work. Is it because by using this last statement confilicts with the while ($guess = <STDIN>) statement? I've kept some comments in as they show my different attempts to make this work. Thank you for any advice you can offer on my problem. I'm sure there are leaner ways to design this program, but I am just trying to understand the variables here rather than make it lean and mean.use strict; my $target = 12; print "Guess my number.\n"; #my $guess = <STDIN>; # Attempt 2 with the "next" statements below co +mmented out. This failed also. print "Enter your guess: "; #my $guess = <STDIN>; Attempt 1 worked but had to enter the guess a s +econd time on a new line before processing my $guess; while ( $guess = <STDIN>) { if ($guess == $target) { print "You guessed it!"; last; }elsif ($guess > $target) { print "Your guess is too high.\n"; #next; # Attempt 1 using this failed. }elsif ($guess < $target) { print "Your guess is too low.\n"; #next; # Attempt 1 using this failed. } print "Enter your guess: "; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regarding Declaring Variables
by GrandFather (Saint) on Nov 17, 2005 at 02:36 UTC | |
by Delusional (Beadle) on Nov 17, 2005 at 09:42 UTC | |
by GrandFather (Saint) on Nov 17, 2005 at 19:40 UTC | |
by ysth (Canon) on Nov 18, 2005 at 12:36 UTC | |
|
Re: Regarding Declaring Variables
by vishi83 (Pilgrim) on Nov 17, 2005 at 05:50 UTC | |
by Tech77 (Novice) on Nov 17, 2005 at 15:40 UTC |