Thanks Keith.  It does help.  I have, however figured out another error
with the script in the next lesson.

#!/usr/bin/perl
%words = qw(
    fred          camel
    barney        llama
    betty         alpaca
    wilma         alpaca
);
print "What is your name? ";
$name = <STDIN>;
chomp ($name);
$original_name = $name;                   # save for greeting
$name =~ s/W.*//;                         # get rid of everything after
first word (according to the book)
$name =~ tr/A-Z/a-z/; # lowercase everything
print "The name converted to: $name\n";   # this (according to the book)
should just be the first name.
if ($name eq "randal") { # ok to compare this way now
    print "Hello, Randal!  How good of you to be here!\n";
} else {
    print "Hello, $original_name!\n";     # ordinary greeting
    $secretword = $words{$name};          # get the secret word
    print "$secretword\n";
    if ($secretword eq "") {              # oops, not found
        $secretword = "groucho";          # sure, why a duck?
    }
    print "What is the secret word? ";
    $guess = <STDIN>;
    chomp ($guess);
    while ($guess ne $secretword) {
        print "Wrong, try again.  What is the secret word? ";
        $guess = <STDIN>;
        chomp ($guess);
    }
}

This script expands on the previous script and in fact causes unexpected
results.  The book explains that s/W.*// will take the first
non-character (A-z, 0-9, -_) and remove it and everything to the left of
it.  To me, that would include spaces, but that is not the behavior I am
getting on my machine.  If I enter Fred FLINTstone, the print "The name
converted to: $name\n"; statement, which I added to the script, prints
"fred flintstone", therefore it does not match the array $words.

I am now assuming the space character is allowed as part of the
expression s/W.*//.


Tom Carroll
Dataware Computers

-----Original Message-----
From: Keith Woody [mailto:[EMAIL PROTECTED]] 
Sent: Friday, May 10, 2002 1:21 PM
To: Tom Carroll
Cc: [EMAIL PROTECTED]
Subject: Re: [e-smith-devinfo] Perl programmers - help for a lowly
unworthy newbie


Tom,
the -w flag just tells perl to _w_arn you verbally about errors, even if
they aren't fatal.

the error below is referring to the following line of code
...
if ($secretword eq "") {     # oops, not found
...

in the case where you type "Fred" or "FRED" or anything that isn't
"fred", $secretword is empty (uninitialized) because %words only has an
entry for "fred". Since $secretword is empty, it sets it to "groucho",
forcing you to use that to exit.

Hope this helps,

-Keith
<keith snip>
"Use of uninitialized value in string eq at ./hello_world_6.pl line 16
<STDIN> line 1."
</keith snip>


--
Please report bugs to [EMAIL PROTECTED]
Please mail [EMAIL PROTECTED] (only) to discuss security issues
Support for registered customers and partners to [EMAIL PROTECTED]
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
Archives by mail and http://www.mail-archive.com/devinfo%40lists.e-smith.org

Reply via email to