Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Another quick question! Is there a shorthand way to test multiple variables against the same criteria within an IF statement where only 1 of the variables needs to match for it trigger the statement? For example, I would normally use:

if ($var1 == 1 || $var2 == 1 || $var3 == 1 || $var4 == 1) {

To do something like this - but it's quite longwinded. What i'm after is a shorter way to test whether any of those variables == 1.

  • Comment on Testing multiple variables against the same criteria (IF statement)
  • Download Code

Replies are listed 'Best First'.
Re: Testing multiple variables against the same criteria (IF statement)
by NetWallah (Canon) on Sep 05, 2016 at 17:56 UTC
Re: Testing multiple variables against the same criteria (IF statement)
by BrowserUk (Patriarch) on Sep 05, 2016 at 17:57 UTC

    You could do:

    if( grep{ $_ == 1 } $var1, $var2, $var3, $var4 ) {

    But that won't short-circuit -- ie. It will test all 4 variables every time -- and is really only beneficial if the vars are an array.

    You might consider List::MoreUtils::any() which will short-circuit.

    Personally, if there are only four variables and using an array doesn't work for you; I'd stick with your original statement.


    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". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Testing multiple variables against the same criteria (IF statement)
by shmem (Chancellor) on Sep 05, 2016 at 21:36 UTC
    What i'm after is a shorter way to test whether any of those variables == 1.

    If by shorter you mean "golfed down", i.e. achieving the same with less characters typed - it is up to you to be smart, go ahead, be clever.. but if by shorter you mean less convoluted, I'd propose to wrap the test into a loop which aliases each var, more so if there's a variable amount of many variables to test, and it is possible to wrap them into an array (which should be possible but in very weird cases):

    $test_succeeded = 0; for(@vars_to_test) { 1 == $_ and ++$test_succeeded and last; } if ($test_succeeded) { ... }
    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

      Or even:

      c:\@Work\Perl>perl -wMstrict -le "my @ra = (0, 0, 0, 1, 0); ;; my $test_succeeded; $test_succeeded = 1 == $_ and last for @ra; ;; print 'succeeded!' if $test_succeeded; " succeeded!


      Give a man a fish:  <%-{-{-{-<

Re: Testing multiple variables against the same criteria (IF statement)
by haukex (Archbishop) on Sep 05, 2016 at 18:32 UTC

    Hi Anonymous,

    You could have a look at the modules Quantum::Superpositions (I love the name ;-)) or Perl6::Junction:

    use Quantum::Superpositions 'any'; # - or - #use Perl6::Junction 'any'; my ($var1,$var2,$var3,$var4) = (0,0,0,0); if ( any($var1,$var2,$var3,$var4) == 1 ) { print "Bingo!\n"; }

    Of course, if you have variables named $var1,$var2,...,$varN, you really should be using arrays...

    my @ary = (0,0,0,0); if ( any(@ary) == 1 ) { print "Bingo2!\n"; }

    Hope this helps,
    -- Hauke D

Re: Testing multiple variables against the same criteria (IF statement)
by hippo (Archbishop) on Sep 05, 2016 at 20:40 UTC