Hey folks,
New to perl, new to perlmonks. But this time I am motivated enough to believe that I'll finally get a handle on the beast! Dare I say I _love_ learning regular expressions?
Okay before I get too verbose, I should say that I believe (not sure) I'm using 5.6.1. (tried manning to find perl version, but no luck - also checked perlfaq). The book I am learning from is Laura Lemay's "Perl In 21 Days" by sams. The code example is one I entered by hand from the book and it can be found in the "Day 9" lesson.
I have stared at this code 'till my eyes started bleeding and I think I need a fresh pair (hopefully those of a perlmonk) to help me figure this one out.
The code that's troubling me in specific is this:
elsif (/[W_]/) { # other chars
print "huh? That *really* doesn't look like a number
+\n";
}
Which should print out the annoying message in the print command. It does not. I know not why. Otherwise, it compiles and works "fine". I could whine about the things I tried, but I'm trying (unsuccessfully) to keep this short.
The script is as follows:
#!/usr/bin/perl -w
# number speller: prints out word approximations of numbers
# simple version, only does single digits
$exit = ""; # whether or not to exit the script
while ($exit ne "n") {
while () {
print 'Enter the number you would like to spell (0-9): ';
chomp($_ = <STDIN>);
if (/^\d$/) {
print "Thanks!\n";
last;
} elsif (/^$/) {
print "You didn't enter anything.\n";
} elsif (/\D/) { # nonnumbers
if (/[a-zA-Z]/) { # letters
print "You can't fool me. There are letters in there.
+\n";
} elsif (/^-\d/) { # negative numbers
print "That's a negative number. Positive only, plea
+se!\n";
} elsif (/\./) { # decimals
print "That looks like it could be a floating point
+ numer.\n";
print "I can't spell a floating point number. Try a
+gain.\n";
} elsif (/[W_]/) { # other chars
print "huh? That *really* doesn't look like a number
+\n";
}
} elsif ($_ > 9) {
print "Too big! 0 through 9, please.\n";
}
}
print "Number $_ is ";
/1/ && print 'one';
/2/ && print 'two';
/3/ && print 'three';
/4/ && print 'four';
/5/ && print 'five';
/6/ && print 'six';
/7/ && print 'seven';
/8/ && print 'eight';
/9/ && print 'nine';
/0/ && print 'zero';
print "\n";
while () {
print 'Try another number (y/n)?: ';
chomp ($exit = <STDIN>);
$exit = lc $exit;
if ($exit =~ /^[yn]/) {
last;
}
else {
print "y or n, please\n";
}
}
}
All of the other options in the while loop that tests the input (using regexes) after the "nonnumbers" test:
} elsif (/\D/) { # nonnumbers
seem to work fine. It's just that one that's balking? Any ideas? I'm also open to comments about how to post more effectively/better since this is my first EVER post to this site.
THANK YOU!
janitored by ybiC: Retitle from less-than-descriptive "5.6.1 begginner newb question", balanced <readmore> tags around codeblock, minor format tweaks for legibility
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.