Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Multiple Conditional Statements

by BillKSmith (Monsignor)
on Sep 11, 2013 at 15:01 UTC ( [id://1053507]=note: print w/replies, xml ) Need Help??


in reply to Multiple Conditional Statements

If you like a more direct approach,
use strict; use warnings; use List::MoreUtils qw(any); my ($RB1, $RB2) = (1,2); my ($WR1, $WR2) = (3,4); my $TE1 = 1; my $match; my @a = \($RB1, $RB2, $WR1, $WR2, $TE1); for my $i (0..3){ $match |= any {${$a[$i]} == $$_} @a[$i+1 .. 4]; } print "No " if !$match; print "match\n";
You could use grep in place of any (List::MoreUtils), but the name 'any' seems to convey the intent better.
Bill

Replies are listed 'Best First'.
Re^2: Multiple Conditional Statements
by BrowserUk (Patriarch) on Sep 11, 2013 at 15:14 UTC
    If you like a more direct approach,

    That's a perfectly valid approach, but how is it "more direct"? (And "more direct" that what for that matter?)

    You have:

    1. An auxiliary boolean variable;
    2. An auxiliary array (... of references which have to be taken);
    3. Two nested loops;
    4. A callback function (from a module);
    5. An array slice;
    6. n(n+1) dereferences;
    7. n(n+1)/2 comparisons;
    8. And it doesn't even short-circuit if the first two variables compared are the same.

    That's the strangest definition of "more direct" I can think of :)


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Of course I agree with all your arguments. All I meant to imply was that it directly compares the variables in question (not copies) and with very little source code. I find it very easy to understand. In general, that should trump issues of speed, memory, or namespace.
      Bill
        I find it very easy to understand. In general, that should trump issues of speed, memory, or namespace.

        Interesting justifiction. Personally, I prefer solutions that are:

        1. Clean.

          Don't create persistent, named temporaries to clutter, conflict and confuse the purpose of the code.

        2. Clear.

          Don't introduce unnecessary (and counterproductive) levels of indirection in the name of premature optimisation (avoiding copies).

          It is more expensive to takes references then dereference those references; than to make copies of simple, small scalar values:

          ($a,$b,$c,$d,$e) = 1 .. 5; cmpthese -1,{ a=>q[ my @a = ($a,$b,$c,$d,$e); ++$_ for @a;], b=>q[my @a = \($a,$b,$c,$d,$e); ++$$_ for @a; ] };; Rate b a b 525790/s -- -23% a 685872/s 30% --
        3. Concise.

          The goal of this snippet if to cross-compare a few variables with the only interest being a single boolean truth.

          Spreading that across half a dozen lines, adding named temporaries to the mix; adding a module, a callback and nested loops; and obscure indirections to derive a simple boolean does the very opposite of making things easy to understand.

        4. Simple.

          Simple means when the algorithm calls for:

          if( <some condition> ) { ## do somthing }

          the implementation (code) should reflect that. Not detract from the algorithm by introducing unneeded complexity.

        5. Efficient.

          looping to perform all 15 comparisons when the first or the second can resolve the boolean condition is just flagrant waste.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1053507]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-26 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found