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?
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.