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


In reply to Character class question by bluethundr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.