uksza has asked for the wisdom of the Perl Monks concerning the following question:
Hello Wise Monks,
Is it good way?
$test = 7;
#(OK only when $test is 7,5 or 3)
#OK, but... long
print "OK\n" if ($test == 7 or $test == 5 or $test == 3);
#NOT OK
#print "OK\n" if ($test == (7 or 5 or 3));
#OK?
print "OK\n" if ($test =~ /[753]/);
Last try is good? Rather not (what about 55, 33 etc?)
Is any good (not switch/case) method to test it?
Thanx a lot
uksza
Re: IF and some conditions
by thcsoft (Monk) on Jun 02, 2005 at 11:41 UTC
|
there are a lot of ways. you proposed 3 of them, of which the last one looks the most elegant to me. but just for the sake of my vanity here's another approach:
print "OK\n" if grep /$test/, (3,5,7);
;)
language is a virus from outer space.
| [reply] [d/l] |
|
I'd really rather write that with an == test instead of a regular expression:
print "OK\n" if grep { $test == $_ } (3,5,7);
| [reply] [d/l] |
Re: IF and some conditions
by Joost (Canon) on Jun 02, 2005 at 11:40 UTC
|
use Quantum::Superpositions;
if ($x == any($a, $b, $c)) { ... }
Update: In this specific case, that would translate to:
use Quantum::Superpositions;
$test = 7;
if ($test == any(7, 5, 3)) {
print "\$test is ",any(7, 5, 3),"\n";
}
__OUTPUT__
$test is any(3,7,5)
| [reply] [d/l] [select] |
Re: IF and some conditions
by wfsp (Abbot) on Jun 02, 2005 at 11:51 UTC
|
my %hash = map {$_ => undef} qw(7 5 3);
print "ok" if exists $hash{$test};
| [reply] [d/l] |
|
use Set::Object;
my $set = set(qw(7 5 3));
print "ok" if $set.includes($test);
This module is called plain "Set" in Perl 6.
$h=$ENV{HOME};my@q=split/\n\n/,`cat $h/.quotes`;$s="$h/."
."signature";$t=`cat $s`;print$t,"\n",$q[rand($#q)],"\n";
| [reply] [d/l] [select] |
Re: IF and some conditions
by jhourcle (Prior) on Jun 02, 2005 at 11:54 UTC
|
#NOT OK
print "OK\n" if ($test =~ /[753]/);
I think you were on the right track with why the last one wasn't good. Try with any of the following input, and you'll see why: 50, 72, 39, 256. You want to anchor your regexes when testing for equality:
print "OK\n" if ($test =~ /^[753]$/);
Update: I can't spell. | [reply] [d/l] [select] |
Re: IF and some conditions
by tlm (Prior) on Jun 02, 2005 at 13:45 UTC
|
Here's another solution, using first (to stop searching as soon as possible):
use List::Util 'first';
print "OK\n" if defined first { $test == $_ } (7, 5, 3);
| [reply] [d/l] |
Re: IF and some conditions
by aukjan (Friar) on Jun 02, 2005 at 11:54 UTC
|
$test = 7;
@array = qw(3 5 7);
foreach ( @array ){ print $test == $_ ? 'OK':undef;}
.:| If it can't be fixed .. Don't break it |:.
| [reply] [d/l] |
Re: IF and some conditions
by Ben Win Lue (Friar) on Jun 02, 2005 at 14:52 UTC
|
for rather small values that are OK,
this seems to be the fastest solution:
my @ok=(0,0,0,1,0,1,0,1);
# 0 1 2 3 4 5 6 7
my $x = 4;
my $y = 5;
if($ok[$x]){print "$x is ok\n";}
if($ok[$y]){print "$y is ok\n";}
if your OK-values get larger, I would use a hash instead of an array!
| [reply] [d/l] |
Re: IF and some conditions
by nedals (Deacon) on Jun 02, 2005 at 16:01 UTC
|
if ($result =~ /^(3|33|5|17)$/) { print "it's there\n"; }
| [reply] [d/l] |
Perl6 Junctions
by mugwumpjism (Hermit) on Jun 03, 2005 at 06:48 UTC
|
In perl 6, Junctions are a core language feature.
So, you could write:
say "OK" if ($test == any(7,5,3));
Thankfully someone has already implemented them for Perl 5, with the module on CPAN called Perl6::Junctions;
use Perl6::Junctions qw(any);
print "OK\n" if ($test == any(7,5,3));
This is actually very similar to the Quantum::Superpositions answer above, and I believe the implementations share a common root.
$h=$ENV{HOME};my@q=split/\n\n/,`cat $h/.quotes`;$s="$h/."
."signature";$t=`cat $s`;print$t,"\n",$q[rand($#q)],"\n";
| [reply] [d/l] [select] |
|
|