Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Character class question

by bluethundr (Pilgrim)
on Apr 25, 2004 at 20:29 UTC ( #348009=perlquestion: print w/ replies, xml ) Need Help??
bluethundr has asked for the wisdom of the Perl Monks concerning the following question:

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

Comment on Character class question
Select or Download Code
Re: Character class question
by dragonchild (Archbishop) on Apr 25, 2004 at 20:37 UTC
    To find your Perl version, type "perl -v" from any commandline. This will work on pretty much any system Perl can be installed onto.

    As for your regex, I'll explain what you're doing. You're matching against 'W' (that's the capital letter) and '_' (underscore). You probably want a '\' (backslash) in front of the W. :-)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

Re: Character class question
by chromatic (Archbishop) on Apr 25, 2004 at 20:37 UTC

    Welcome to the site.

    You're probably missing a backslash in front of the W. \W signifies the character class containing non-word characters -- everything but a-zA-z0-9 and the underscore, though.

    (As for posting suggestions, a better title would describe the problem you're having, not your status as a user. Including the version of Perl and the book and exercise you're using was wise, though.)

Re: Character class question
by matija (Priest) on Apr 25, 2004 at 20:38 UTC
    perl -v will show the version of Perl you are running.

    As for your regexp question, you are missing a backslash (\) before the letter "W". So, the propper line would be

    } elsif (/[\W_]/) { # other chars
    That would match anything that is not a number (0-9) or a character - but the characters a-z are captured by the if higher up in the code. (Note that this will have problems if input contains Unicode characters)...
      Thanks, to you and the dudes above sharing their wisdom! perl -v yielded 5.8.0. The info page said it was for 5.6.1. Now that I know this simple trick, I'll be able to be more accurate in my titles.

      As to the code, yup. I'm missing the backslash before the 'W'. Hopefully I'll become more adept at catching these errors as time goes on and my experience builds.THANKS again for the code advice and the posting advice to ALL!

        OK, now that we know what version you're running, go ahead and add these things at the top of your programs for the time being:

        use strict; # Helps to prevent a few common coding + # "mistakes" use warnings; # Turns on "lexically-scoped" warnings, # a bit better than -w use diagnostics; # Makes error messages more helpful

        If I had known about Diagnostics from the beginning, I would have saved myself so much work... ;)

        Update: Fixed improper capitalization as pointed out by the nice AM.

        --
        Damon Allen Davison
        http://www.allolex.net

Re: Character class question
by qq (Hermit) on Apr 26, 2004 at 12:11 UTC

    I'd recommend Learning Perl by the monastory's merlyn. The more Perl books you read, the better. One of the nicest things about programming with Perl is that you never run out of things to learn.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://348009]
Approved by barrd
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2014-10-31 04:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (214 votes), past polls