>I couldn't quite follow this program off the book. The parts not quite
>clear to me are (the hwole program is given below):
I'll go through them block by block, and then you can ask deeper questions
about the ones that I don't explain well enough.
>$i = 0;
I'm guessing that you know that $i is a variable. Here we set it to 0.
This is because we are going to count with it later in the program.
>$correct = "maybe"; {
We're again initializing a variable, in this case $correct has the value
"maybe" assigned to it.
>while ($correct eq "maybe")
>{
>if ($words[$i] eq $guess){
>$correct = "yes";
>}
As long as $correct still has the value "maybe", look at element $i of the
@words array and compare it to the value of the variable $guess, if they are
the same, change the value of $correct to "yes", thereby breaking us out of
the while loop.
>elsif ($i < 2) {
>$i = $i + 1;
>}
If the $i th element of @wordss is not the same as guess and the value of $i
is 0 or 1, then we add 1 to $i. This is because @words is hardcoded with 3
responses, and we're using $i to loop through the @words array in the if.
It also helps to indent everything within a set of braces {} so that you can
see where loops and conditionals begin and end:
----------------------the whole program-----------------------
#!/usr/bin/perl
@words = qw (llama camel alpaca);
print "what is your name\n";
$name = <stdin>;
chomp $name;
if ($name eq "tan") {
print "hey man\n";
} else {
print "hi, $name\n";
print "enter the password\n";
$guess = <stdin>;
chomp $guess;
$i = 0;
$correct = "maybe"; {
while ($correct eq "maybe") {
if ($words[$i] eq $guess){
$correct = "yes";
} elsif ($i < 2) {
$i = $i + 1;
} else {
print "wrong. try again\n";
$guess = <stdin>;
chomp $guess;
$i = 0;
}
}
}
}
#EOF.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]