"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 Need Help??

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])/);

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://516849]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-02-29 18:49 GMT
Voting Booth?
My favourite way to spend a leap day ...

Results (27 votes). Check out past polls.