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

Performing numeric compare in an if statement

while (<$FILE> { chomp; my ($number1,$number2,$number3,$number4) = split /\t/; print "$number4\n"; if ($number4 == 1,2) { print "$number4 good\n"; } else { print "$number4 bad\n"; } }

I've correct result, but there are warnings

Useless use of numeric eq <==> in void context at .... Argument "1,2" isn't numeric in numeric eq <==> at .... 1,2 good

If I replace "==" with "eq" then i've warning and only 1 get printed. Expected output is 1,2 good

useless use of string eq in void context at .... 1 good

If I replace "eq" with "=" then i've no warning and only 1 get printed. Expected output is 1,2 good

1 good

Don't understand what's wrong. Please help!

Replies are listed 'Best First'.
Re: Perl compare
by davido (Cardinal) on Sep 26, 2014 at 02:30 UTC

    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

      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

Re: Perl compare ( numeric in void isn't numeric)
by Anonymous Monk on Sep 26, 2014 at 01:05 UTC

    Don't understand what's wrong. Please help!

    There are checklists for that , Basic debugging checklist

    Work through the list, turn on diagnostics, read the more verbose error message, use Deparse to see how perl parses your code

    "1,2" is not "a number" or "a string" , its "a list", its two numbers seperated by the comma operator

      "1,2" is not "a number" or "a string" , its "a list", its two numbers separated by the comma operator

      it's a list

      my ($num1,$num2) = split (',',$number4); if ($num1 == 1 && $num2 == 2) {....

      above code have no warning

        It seems $number4 is not a list, it's a string. So, use string comparison eq and quote the string literals:
        if ($number4 eq '1,2') { # ... }
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        No, a scalar can not be a list. It is a string containing two digits separated by a comma.

        1 Peter 4:10
Re: Perl compare
by Jenda (Abbot) on Sep 26, 2014 at 10:02 UTC

    Numeric literals in Perl and any other programming language I've ever heard of use decimal dots, not commas. Irrespective of locale settings.

    if ($number4 == 1.2) {

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

Re: Perl compare
by Random_Walk (Prior) on Sep 26, 2014 at 14:18 UTC

    You already have some great advice on fixing your code. There is one thing I would like to add. When you have a construct like this: my ($number1,$number2,$number3,$number4) = split /\t/; it is time to learn about arrays.

    use strict; use warnings; ... my @numbers = split /\t/; if ($number[3] == 42) { print "I got the answer\n"; } else { print "What was the question?\n"; } ...
    Note Perl arrays start with index 0 as the first element, so $numbers3 is the fourth number. You'll get used to it.

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
Re: Perl compare
by Anonymous Monk on Sep 26, 2014 at 15:34 UTC

    waytoperl, as you can tell you're getting very varied responses depending on different interpretations of what you've posted here. What is missing is the full output, specifically that of print "$number4\n";, as well as a sample of the input data. Please read and follow the advice in How do I post a question effectively?

      My issue got resolved. It was list and used split to resolve. Thank you all.