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
}
}
}
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
}
}
}