http://qs321.pair.com?node_id=1170412


in reply to Check multiple array elements for a single condition

You could also use the Smart::Match module with smartmatching:
use Smart::Match 'all'; if ( 0 ~~ all(@array[0,1]) ) { say 'matched'; }
Or you could do exactly the same thing "natively", in Perl 6:
if @array[0 & 1] == 0 { say 'matched'; }
Damian

Replies are listed 'Best First'.
Re^2: Check multiple array elements for a single condition
by Linicks (Scribe) on Aug 25, 2016 at 18:12 UTC
    Just thinking - what about:

    if (!(@array[0]+@array[1]))

    Nick
      if (!(@array[0]]+@array[1]))

      False positive if $array[0] == -$array[1].

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        Please explain what you mean by 'false positive'?

        Thanks, Nick

      Try if (!(@array[0] | @array[1])) instead. (For reasons afoken stated.)

        Try if (!(@array[0] | @array[1])) instead

        Don't, for the reason Marshall++ explained. Micro-optimising code to avoid typing a few characters does not improve performance, and makes maintenance a nightmare (if overused).

        Also, your code assumes integers, right? I would have to look up the binary representation(s) of a float 0.0 to see if all bits are 0 there. If not, this probably won't work, resulting once again in wrong results (false negatives, in this case). Maybe floats depend on the CPU and / or math emulation library used, so the result would also depend on the machine it is running on. No, thanks. I prefer code that does not make me think about such issues.

        For two or three elements, explicitly compare the elements. For more, see my generalised posting.

        Playing golf is a completely different story, but then, the array's name would not be five letters long.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re^2: Check multiple array elements for a single condition
by Anonymous Monk on Sep 10, 2016 at 16:00 UTC
    Isn't Smart Match deprecated?