LaylaNahar
Programmer
Hi - I copied this script from Oreilly's 'Learning Perl'.
I'm pretty sure I copied it accurately, but the logic doesn't work. when you don't guess the secret word, the elsif clause is executed twice in a row, advancing the index to the end of the array, instead of just on to the next item.
I'm wondering if my unfamiliarity with perl is making it difficult for me to spot an obvious error. Can anybody tell why it does this? Thanks.
I'm pretty sure I copied it accurately, but the logic doesn't work. when you don't guess the secret word, the elsif clause is executed twice in a row, advancing the index to the end of the array, instead of just on to the next item.
I'm wondering if my unfamiliarity with perl is making it difficult for me to spot an obvious error. Can anybody tell why it does this? Thanks.
Code:
#!/usr/bin/perl -wT
@words = ("lama", "alpaca", "camel");
print ("guess the secret word\n");
$guess = <STDIN>;
chomp ($guess);
$i = 0;
$correct = 'maybe';
while ($correct eq "maybe") {
if ($words[$i] eq $guess ) {
$correct = "yes"; # get out of while
}
elsif ($i < 2) {
$i=$i+1;
}
else {print ("Try again please\n");
$guess = <STDIN>;
chomp ($guess);
$i=0;
}#end else
}# end of while