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

Hi, can someone please help me with the code below and how to use curly brackets. Thank you for your time

#! usr/bin/perl -w $im_thinking=int(rand 10); print "Pick a number"; $guess=<STDIN>; chomp $guess; while($guess != $im_thinking) if($guess>$im_thinking) print "You guessed too high!\n"; elsif($guess < $im_thinking) print "You guessed too low!\n"; else print "You got it right!\n";

Replies are listed 'Best First'.
Re: Help with While Loop
by choroba (Cardinal) on Sep 14, 2015 at 11:37 UTC
    Curly braces are needed around blocks. In the pythonesque code you posted, a left brace is missing whenever the indentation increases, and the right brace is missing whenever it decreases.

    Unfortunately, putting the curly braces in doesn't make the code work. The user's input must be repeated in the loop, too, otherwise the code would test the same number again and again.

    #! /usr/bin/perl use warnings; use strict; my $im_thinking = int rand 10; print "Pick a number: "; my $guess = -1; while ($guess != $im_thinking) { $guess = <STDIN>; chomp $guess; if ($guess > $im_thinking) { print "You guessed too high!\n"; } elsif ($guess < $im_thinking) { print "You guessed too low!\n"; } } print "You got it right!\n";

    Update: You can use last and the ternary operator to make the code even simpler:

    #! /usr/bin/perl use warnings; use strict; my $im_thinking = int rand 10; print "Pick a number: "; while () { my $guess = <STDIN>; chomp $guess; last if $guess == $im_thinking; print "You guessed too ", $guess < $im_thinking ? 'low' : 'high', "\nTry again: "; } print "You got it right!\n";
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      In the pythonesque code you posted...

      Not only Pythonesque, but perhaps also a bit C-ockamamie. The following works, although you have to torture the code to get the syntax right (and the indentation is meaningless). And it still doesn't do anything.

      #include <stdio.h> void main () { int x = 5; int y = 6; while (x != y) if (x > y) printf("guessed %d: high. ", x), printf("next guess %d \n +", x = y); else if (x < y) printf("guessed %d: low. ", x), printf("next guess %d \n" +, x = y+1); else printf("got it! \n"); printf("done guessing \n"); }
      Output:
      c:\@Work\GCC\PerlMonks\gavin100>a guessed 5: low. next guess 7 guessed 7: high. next guess 6 done guessing

      Update: Changed node title. This really is OT.


      Give a man a fish:  <%-{-{-{-<

      Thats brilliant, thank you for your time and assistance

Re: Please help
by Corion (Patriarch) on Sep 14, 2015 at 11:31 UTC

    Where do you have problems with the code you've shown?

    I would suggest reviewing your course materials, especially where they mention the syntax of while and if statements.

    For example, perlsyn documents the following structure for a while statement:

    LABEL while (EXPR) BLOCK

    Maybe you can apply that to your problem?

      Thank you

Re: Help with While Loop
by locked_user sundialsvc4 (Abbot) on Sep 14, 2015 at 16:33 UTC

    Also note that the Perl language uses different operators to compare strings for equality or inequality:   eq, ne, cmp ... see perlop.

    Operators such as ==, !=, <=&t; always perform numeric comparison.   Your use of that operator, then, could be part of the problem.

    And, by the way, this is a very-conscious design decision made by the original architect(s) of the language.   Perl has always been oriented toward the processing of text files, and this strategy makes it possible to precisely specify, say, sorting operations and so forth when the data being processed (in the text ...) “is a number.”