learning perl, 2nd ed., 1st problem not working.

J

jason.helfman

I am not certain why this is not working. I will continually be asked
for another secret wod, eventhough I am giving the right word. The
default of "groucho," works, and for the user not having to give one,
but for anyone in the list, it doesn't work. At one point in the
chapter it did work, but not now, and I am not certain what has
changed.

Nothing in the errata on the book seems to point to a resolution,
either.

Thanks,
Stumped.


#!/usr/bin/perl -w
init_words();
print "What is your name? ";
$name = <STDIN>;
chomp ($name);
if ( $name =~ /^jason\b/i) {
print "Hello, Jason. How nice it is to meet you!\n";
} else {
print "Hello, $name. Now, I ask ze Questions!\n";
print "What is the secret word? ";
$guess = <STDIN>;
chomp ($guess);
while (! good_word($name,$guess)) {
print "Wrong, try again. What is the secret word? ";
$guess = <STDIN>;
chomp ($guess);
}
}

sub init_words {
while ( defined($filename = glob("*.secret")) ) {
open (WORDSLIST, $filename) || die "can't open wordlist; $!";
if (-M WORDSLIST >= 7.0) {
while ( $name = <WORDSLIST>) {
chomp ($name);
$word = <WORDSLIST>;
chomp ($word);
$words{$name} = $word;
}
}
close (WORDSLIST) || die "couldn't close wordlist: $!";
}

sub good_word {
my($somename,$someguess) = @_; # name the parameters
$somename =~ s/\W.*//; # get rid of everything after the first word
$somename =~ tr/A-Z/a-z/; # convert to lowercase
if ($somename eq "jason") {
return 1; # return value is true
} elsif (($words{$somename} || "groucho") eq $someguess) {
return 1; # return value is true
} else {
# open MAIL,"|mail -s 'error in guess' jhelfman\@truecredit.com";
# print MAIL "bad $somename guessed $someguess\n";
# close MAIL;
return 0; # return value is false
}
}
}
 
P

Paul Lalli

I am not certain why this is not working. I will continually be asked
for another secret wod, eventhough I am giving the right word. The
default of "groucho," works, and for the user not having to give one,
but for anyone in the list, it doesn't work. At one point in the
chapter it did work, but not now, and I am not certain what has
changed.

} elsif (($words{$somename} || "groucho") eq $someguess) {
return 1; # return value is true

Take a closer look at your parentheses. You are testing if
($words{$somename} || "groucho")
is equal to
$someguess.

You *want* to be testing if
$words{$somename}
is true, or
"groucho" eq $someguess

Paul Lalli
 
J

jHELL

I couldn't get the syntax right to do what you were suggesting, but I
did totally comment out that statement group and I am having the same
issue.
 
D

Daniel Cutter

I couldn't get the syntax right to do what you were suggesting, but I
did totally comment out that statement group and I am having the same
issue.

As Paul wrote, regroup:
} elsif ($words{$somename} || ("groucho" eq $someguess)) {


Daniel
 
J

jHELL

Ok. Thanks.

It was working, before though, with the same results. Maybe this logic
makes more sense, though.

Either way, it still is asking me for the secret word, eventhough I am
putting in the right word for anyone but the default user.

-Jason
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top