Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Using IF and OR, I'm sure there is a better way

by l.frankline (Hermit)
on Dec 15, 2005 at 04:09 UTC ( [id://516849] : note . print w/replies, xml ) Need Help??


in reply to Using IF and OR, I'm sure there is a better way

Here is an another possible way...

For single numeric values:

     print $& if ($variable=~/[123]/);

For two or more numeric values:

     print $& if ($variable=~/[1][0]+/);

Also, take a look at perlrequick

Regards
Franklin

Don't put off till tomorrow, what you can do today.

Replies are listed 'Best First'.
Re^2: Using IF and OR, I'm sure there is a better way
by l3v3l (Monk) on Dec 15, 2005 at 22:02 UTC
    these will work even if it is a double digit in $var (ie: 1 and 10 will both succeed for the first example) but if you only want one digit in range maybe something like:
    print $& if $var =~ /^[1-3]$/;
    or ... better yet ... if you are going to be doing this alot and with strings as well as digits ...
    sub look_into { my $archetype= shift; eval 'sub { $_[0] =~ /$archetype/o; }' } my $is_text_am_too_red = look_into q/\b(am|too|red)\b/; my $single_digit_in_range = look_into q/^[1-5]$/; my $double_digit_in_set = look_into q/^[5-7][49]$/; # etc. if($is_text_am_too_red ->($_)){ print "get some sleep!" } if($single_digit_in_range ->($_)){ print "single digit in range!" } if($double_digit_in_set ->($_)){ print "double digit in set!" } #etc.
Re^2: Using IF and OR, I'm sure there is a better way
by ikegami (Patriarch) on Dec 21, 2005 at 18:05 UTC

    Using $& can slow down regexps elsewhere in the program, so replace
    print $& if ($variable=~/[123]/);
    with
    print $1 if ($variable=~/([123])/);