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

#!\usr\bin\perl -w my $secret= int(1+ rand 100) while(1){ print"please enter a number:"; chomp(my $guess=<STDIN>); if($guess=~/quit|exit|^\s*$/i){ print"Sorry,the number was $secret.\n"; last; } elsif($guess>$secret){ print "Too large,try again.\n"; last; } elsif($guess==$secret){ print"that is it.\n"; last; }else{print"Too small,try again.\n"; } } } }

when i run it,the systerm remind me that there are syntax at the line 3,near"){" and at the line 17,near"}",i don't know why,who can help me.

2004-11-11 Edited by Arunbear: Changed title from 'help', as per Monastery guidelines

Replies are listed 'Best First'.
Re: Syntax error near ){
by bgreenlee (Friar) on Nov 11, 2004 at 01:40 UTC

    You're missing a semicolon after my $secret= int(1+ rand 100)

    -b

Re: Syntax error near ){
by erix (Prior) on Nov 11, 2004 at 01:44 UTC

    And at the end, there are two '}' too many, I think.

    It would be easier if you used codetags, biofeng918. I mean, put <code> before your code and </code> after it.

Re: Syntax error near ){
by NetWallah (Canon) on Nov 11, 2004 at 01:49 UTC
    You were using last() gratuitously, and had too many "}". If you want to allow guesses for multiple numbers, add a loop around creating a new value for $secret, and re-engineer exit conditions.

    Corrected and formatted code :

    #!\usr\bin\perl -w my $secret= int(1+ rand 100); while(1){ print"please enter a number:"; chomp(my $guess=<STDIN>); if($guess=~/quit|exit|^\s*$/i){ print"Sorry,the number was $secret.\n"; last; }elsif($guess>$secret){ print "Too large,try again.\n"; ##last; }elsif($guess==$secret){ print"that is it.\n"; last; }else{ print"Too small,try again.\n"; } }
    Update:Un-commented "first last" - see hbo's note below.

        Earth first! (We'll rob the other planets later)

      The first last (did I just say that?) is still required to make the loop exit on surrender.

      The second one is correctly in error. (Gotta love them non-sequiters. 8)

      "Even if you are on the right track, you'll get run over if you just sit there." - Will Rogers

Re: Syntax error near ){
by Popcorn Dave (Abbot) on Nov 11, 2004 at 05:15 UTC
    In addition to all the help from the other monks, you should do two more things:
    1. Put use strict; at the top of your program. That will help you catch lots of errors before you run your program.

    2. Indent your code so you can see where the braces should go. Lots of editors allow you to match tags, so you should see if yours does. If not, think about changing to one that does. It will save your sanity. :)

    Useless trivia: In the 2004 Las Vegas phone book there are approximately 28 pages of ads for massage, but almost 200 for lawyers.
Re: Syntax error near ){
by buttroast (Scribe) on Nov 11, 2004 at 05:53 UTC
    Always try to make your topic title as specific as possible, as "help" is not going to help a person who is searching for a similar problem as yours. "syntax error near ){" would probably be a better node title as far as searching is concerned. As others have said, indenting would have also helped you to realize that you had an extra '}'.
    Thanks buttroast