Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Checking for single digit

by Anonymous Monk
on May 06, 2003 at 01:10 UTC ( #255779=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Okay, I figured this would be easy, and if not, would be simple to find, but most of what I'm finding is overkill. I want to check user input for a single digit. 0-9, but not 10, 11, 12, 200, 1a, 2b, etc. I thought that /\d/ only returned a single-digit number, but it's also returning double digits. Here's what I"m working with.
$input = <STDIN> if ($input =~ /\d/) { print "input was a single digit number\n"; } else { print "input wasn't a single digit number\n"; }
I also tried:
$input = <STDIN> if ($input =~ /[0-9]/) { print "input was a single digit number\n"; } else { print "input wasn't a single digit number\n"; }
which works but will return true for input like 2a, 22, 33, 5dfsdf I need it to return true only if the number is single digit. In goofing around, I found that this does work...
my $number; chomp ($number = <STDIN>); if ($number < 10) { print "input was either 0-9 or -\n"; }
...except for something like 2a, which in Perl's eyes is 0, no?

Comment on Checking for single digit
Select or Download Code
Re: Checking for single digit
by vladb (Vicar) on May 06, 2003 at 01:14 UTC
    In your match try to use the special regexp anchors ^ and $:
    if ($input =~ /^[0-9]$/) { print "input was a single digit number\n"; } else { print "input wasn't a single digit number\n"; }

    From the Perl regular expression tutorial (perlre), the "^" and "$" are defined as:
    ^ Match the beginning of the line $ Match the end of the line (or before newline at the end)
    Unless they are used, Perl will look for a match anywhere within your $input string! ;-)

    Update: threw in some perldoc definitions for your viewing pleasure :)

    _____________________
    # Under Construction
Re: Checking for single digit
by TVSET (Chaplain) on May 06, 2003 at 01:14 UTC
    Try to use beginning and end of line markers. Something like this should help:

    if ($input =~ m/^\d$/) { ... }

    or

    if ($input =~ m/^[0-9]$/) { ... }

    It's a good idea also to chomp your input before comparison.

    Leonid Mamtchenkov aka TVSET

      Fantastic! Works like a champ. Sad thing is, I should've thought of that. Guess you have to take a step back every now and again. For the record, I am chomping my stuff, but my browser is really lame on this system (Japanese Redhat 8) and while pasting in here, I got some funk that didn't allow me to see what I was copying from the xterm. Retyped in Kate and pasted again, and simply forgot to type in the chomp. :) Again - thanks. That's exactly what I was looking for.
Re: Checking for single digit
by Enlil (Parson) on May 06, 2003 at 01:23 UTC
    $input =~ /[0-9]/;
    or
    $input =~ /\d/;
    checks to see if $input contains a digit anywhere in the string so that all of the following will match:
    21 dfjdfkfjd2 jf2kjf 9343kfd2
    if you want to insure that the string contains only one digit in it you can do something like the following (it will be true as long as only one thing is a digit no more no less):
    $input =~ /^\D*\d\D*$/;
    on the other hand if you want to make sure the only thing in $input is a single digit then the following will work:
    $input =~ /^\d$/
    which breaks down to finding the following pattern, the ^ anchors that the match starts at the start of the line, and the $ makes sure that the \d is the last thing in the line before the newline character(if it exists).

    -enlil

•Re: Checking for single digit
by merlyn (Sage) on May 06, 2003 at 02:03 UTC
    Hmm. A lot of answers in this thread give something like /^\d$/, but that's wrong, because that also matches things like "1\n".

    You need to use /\A\d\z/, which forces beginning of string and end of string, not $ which means end of string, or just before a newline at the end of string.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      ...but that's wrong...

      There is no such thing :)

      Considering that most peopel don't throw binary data into their input and that chomping is done, /^\d$/ will work properly. And it is way more readable/familiar that /\A\d\z/. But you have a point.:)

      Leonid Mamtchenkov aka TVSET

        I actually like the \A and \z from an aesthetic standpoint. To me they just look much better than ^ and $. Of course, I'll still use whichever one best fits the situation I'm dealing with.
        and that chomping is done,
        Chomping wasn't done here. And none of the other answers (that I saw) mentioned the restrictions on their match.

        Hence, I must point it out.

        Cargo cult is when you use something you don't fully understand, and then pass it on, without understanding or describing its limitations. This was a perfect example of at least three people cargo-culting the answer.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2014-12-28 17:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (182 votes), past polls