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

Aloha Monks! So, I continue on my journey to understand Perl and I still get tripped up by the simplicity of it as I guess I am trying to do this the hard way. I picked up a Perl book and am working through the excercises. I eventually worked this out in a section on loops:
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: "; }
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.

Replies are listed 'Best First'.
Re: Regarding Declaring Variables
by GrandFather (Saint) on Nov 17, 2005 at 02:36 UTC

    Each <STDIN> reads a line from stdin so if you have:

    my $var = <STDIN>; while ($var = <STDIN>)

    you read a line in the declaration (my $var), then replace the value in the while loop while ($var = <STDIN>).

    The next;s inside the elsifs bypass the print "Enter ... at the end of the loop.

    For fun consider this version:

    use strict; my $target = 12; print "Guess my number.\n"; while ((print "Enter your guess: "), my $guess = <STDIN>) { (print "You guessed it!"), last if $guess == $target; (print "You guessed too low\n"), next if $guess < $target; (print "You guessed too high\n"), next if $guess > $target; }

    DWIM is Perl's answer to Gödel
      Just a small alteration changing the hard coded 12 to something random:
      use strict; my $target = sprintf "%.$_[1]f", rand(10); print "Guess my number.\n"; while ((print "Enter your guess: "), my $guess = <STDIN>) { (print "You guessed it!"), last if $guess == $target; (print "You guessed too low\n"), next if $guess < $target; (print "You guessed too high\n"), next if $guess > $target; }
        my $target = 1 + int rand (20); # number 1 - 20

        would be more conventional for generating a random integer. Note that your version generates a number in the range 0 - 9 which may not be what some people would expect.


        DWIM is Perl's answer to Gödel
Re: Regarding Declaring Variables
by vishi83 (Pilgrim) on Nov 17, 2005 at 05:50 UTC

    Hi Tech77 !!

    This is another way to do the same program, though Grandfather had given a better one i guess..

    use strict; use warnings; my $no=12; my $target; do { print "Guess the number : "; chomp($target = <STDIN>); print "$target is too low\n" if ($target < $no); print "$target is too high\n" if ($target > $no); } until ( $target == $no);

    Thank you

    A perl Script without 'strict' is like a House without Roof; Both are not Safe;
      Thank you to vishi83 and GrandFather for the help! Also, to Delusional for the randomization. That's a little more advanced that where I am now, but I'll get there! Your explanations really helped me understand what's going on with my code!