Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Check multiple array elements for a single condition

by Linicks (Scribe)
on Aug 25, 2016 at 18:12 UTC ( [id://1170467]=note: print w/replies, xml ) Need Help??


in reply to Re: Check multiple array elements for a single condition
in thread Check multiple array elements for a single condition

Just thinking - what about:

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

Nick

Replies are listed 'Best First'.
Re^3: Check multiple array elements for a single condition
by afoken (Chancellor) on Aug 25, 2016 at 18:35 UTC
    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
        Please explain what you mean by 'false positive'?
        #!/usr/bin/perl use strict; use warnings; sub are_both_zero { my @array=@_; print "In the array (",join(', ',@array),"), "; if (!($array[0]+$array[1])) { print "both elements are zero.\n"; } else { print "at least one element is not zero.\n"; } } are_both_zero(3,-5); are_both_zero(3,0); are_both_zero(0,-5); are_both_zero(0,0); are_both_zero(3,-3);

        Output:

        In the array (3, -5), at least one element is not zero. In the array (3, 0), at least one element is not zero. In the array (0, -5), at least one element is not zero. In the array (0, 0), both elements are zero. In the array (3, -3), both elements are zero.

        See also https://en.wikipedia.org/wiki/False_positive.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re^3: Check multiple array elements for a single condition
by RonW (Parson) on Aug 26, 2016 at 19:37 UTC

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-03-29 05:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found