in reply to Testing multiple variables against the same criteria (IF statement)

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'
  • Comment on Re: Testing multiple variables against the same criteria (IF statement)
  • Download Code

Replies are listed 'Best First'.
Re^2: Testing multiple variables against the same criteria (IF statement)
by AnomalousMonk (Archbishop) on Sep 05, 2016 at 22:44 UTC

    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:  <%-{-{-{-<