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

hi,,, blazar, check out this code updates:

my $test = $b; $test =~ s/\s+//g; if ($test != 0) { $test =~ s/[1-5]//; print "It is either 1,2,3,4,5\n" if ! $test and $test ne "0"; }

it may seems to be lenghty for me but if you've got shorter codes for this, then you can modify it or create a better one.

What can I say? PerlPhi, please do not take this as a personal offense, I find it overly complex, clumsy, hard to parse and understand. If you really want to go the way of a regex, and I find it quite reasonable, what's wrong with the right™ one?

you may add any additional at @_ to test.

Incidentally, I wouldn't use $b as a general purpose scalar, nor @_ as a general purpose array.

Replies are listed 'Best First'.
Re^7: Quicker way to do this IF statement 1-5
by PerlPhi (Sexton) on May 21, 2007 at 18:06 UTC

    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"; }

      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.