in reply to Perl compare

This line is wrong:

if ($number4 == 1,2) {

I think you probably expected that $number4 will compare to 1 or to 2, and that if it is equal to either 1 or 2 you will have a Boolean "true" condition. But what really happens is that $number == 1 is evaluated, and then the result is thrown away. Then 2 is evaluated, and its value in a Boolean context turns out to be "true". So your conditional will always be true.

Consider this example:

print "Yes!\n" if ( 1 == 2, 3 );

The outcome will be "Yes!", because even though the conditional 1==2 is false, that evaluation is thrown away, and the "3" is evaluated numerically in Boolean context, and that will always be "true".

You probably want to say something like this:

if( grep { $number4 == $_ } 1, 2 ) {...

Or more simply...

if( $number4 == 1 || $number4 == 2 ) { ...

Or more legibly:

use List::Util 'any'; # ......... if( any { $number4 == $_ } 1, 2 ) {...

Dave

Replies are listed 'Best First'.
Re^2: Perl compare
by waytoperl (Beadle) on Sep 26, 2014 at 04:25 UTC
    You probably want to say something like this: if( grep { $number4 == $_ } 1, 2 ) {...
    use List::Util 'any'; # ......... if( any { $number4 == $_ } 1, 2 ) {...

    Will have Argument "1,2" isn't numeric in numeric eq <==> ....

    Or more simply... if( $number4 == 1 || $number4 == 2 ) { ...

    Worked without warning, since $number4 is a list. Thanks