rbees
rbees

Reputation: 35

Beginning Perl syntax error at

Thanks for the help I have found on this site by searching the web when looking for problems.

It seams that I am unable to figure out just what the problem with this code is. I was told that Perl was easier than Bash but now that I have tried it I am not sure that is true at all. Anyway...I have been over this code and searched but have not been able to discover just what is wrong with it. Part of the problem is that my old brain does not learn new things so easy as it once did. The code

#!/usr/bin/perl
use warnings;
use strict;

my $target = 12;
my $guess=0;
print "Guess my number!\n";
print "Enter your guess: ";
$guess = <STDIN>;
while ($guess != $target) {
  if ($target == $guess) {
    print "That's it! You guessed correctly!\n";
    exit;
  }
  elsif ($guess > $target) {
    print "Your number is bigger than my number\n";
    print "Try agin";
    print "Enter a lower guess: ";
    $guess = <STDIN>;
  }
  else ($guess < $target) {  # syntax error at , near "else ("
    print "Your number is less than my number\n";
    print "Try agin";
    print "Enter a higher guess: ";
    $guess = <STDIN>;
  }
}  # syntax error at , near "}"

I am getting errors at the lines marked above. Yet the code is identical to the code in the elsif just above it.

Upvotes: 3

Views: 192

Answers (1)

TLP
TLP

Reputation: 67910

The syntax is else {, since there is no condition for else, whereas elsif does have a condition. The syntax is:

if (condition) {
    ...
}
elsif (condition) {
    ...
}
else {
    ...
}

Upvotes: 6

Related Questions