Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

IF and some conditions

by uksza (Monsignor)
on Jun 02, 2005 at 11:37 UTC ( #462857=perlquestion: print w/ replies, xml ) Need Help??
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

Comment on IF and some conditions
Download Code
Re: IF and some conditions
by Joost (Canon) on Jun 02, 2005 at 11:40 UTC
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.
Re: IF and some conditions
by wfsp (Abbot) on Jun 02, 2005 at 11:51 UTC
    one more
    my %hash = map {$_ => undef} qw(7 5 3); print "ok" if exists $hash{$test};

      If you want a set, then use a set.

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

Re: IF and some conditions
by aukjan (Friar) on Jun 02, 2005 at 11:54 UTC

    And another one
    $test = 7; @array = qw(3 5 7); foreach ( @array ){ print $test == $_ ? 'OK':undef;}


    .:| If it can't be fixed .. Don't break it |:.

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

    the lowliest monk

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!
Re: IF and some conditions
by nedals (Deacon) on Jun 02, 2005 at 16:01 UTC
    One more solution...
    if ($result =~ /^(3|33|5|17)$/) { print "it's there\n"; }
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";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://462857]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (13)
As of 2014-10-22 15:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (119 votes), past polls