in reply to Re^6: Quicker way to do this IF statement 1-5
in thread Quicker way to do this IF statement 1-5

well nothing is personal with it. its just a normal view if other people would see that kind of codes. okey then, the lacking with this regex:

if ($b=~/^[1-5]\z/) { print "\$b has to be a number and is either 1, 2, 3, 4, or 5.\n"; }

is what if $b contains:

my $b = " 5 ";

or

my $b = " \t\t 1 \t ";

then it wont print the message because of the whitespace. but i prefer to that codes (just add a code before "if"):

$b =~ s/\s+//g; #additional code if ($b=~/^[1-5]\z/) { print "\$b has to be a number and is either 1, 2, 3, 4, or 5.\n"; }

Replies are listed 'Best First'.
Re^8: Quicker way to do this IF statement 1-5
by blazar (Canon) on May 21, 2007 at 20:43 UTC

    is what if $b contains:

    my $b = " 5 ";

    Well, the OP explicity stated that she "would like to make sure $b has to be a digit and the digit has to be from 1 to 5". Indeed if you have different requirements and prefer to sanitize the input by removing whitespace, then you can happily do so. Your additional s is perfectly fine, in fact.