82

I am (a complete Perl newbie) doing string compare in an if statement:

If I do following:

if ($str1 == "taste" && $str2 == "waste") { }

I see the correct result (i.e. if the condition matches, it evaluates the "then" block). But I see these warnings:

Argument "taste" isn't numeric in numeric eq (==) at line number x.
Argument "waste" isn't numeric in numeric eq (==) at line number x.

But if I do:

if ($str1 eq "taste" && $str2 eq "waste") { }

Even if the if condition is satisfied, it doesn't evaluate the "then" block.

Here, $str1 is taste and $str2 is waste.

How should I fix this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hari
  • 9,439
  • 27
  • 76
  • 110
  • 5
    Please post a self-contained example. Without knowing what `$str1` and `$str2` are, we can't tell you why control is reaching more code we don't see. To your question, `eq` compares scalars lexicographically so is useful here, while `==` compares numerically so won't meaningfully compare anything to `"taste"`. – Mike Samuel Dec 26 '12 at 21:51
  • @hari Post the exact code where the variables are initialized. If possible, post the complete function or script. – cdhowie Dec 26 '12 at 21:53
  • 1
    It's not the correct result if the "then" block is evaluated even when the condition is false. – ikegami Dec 27 '12 at 03:35

4 Answers4

112

First, eq is for comparing strings; == is for comparing numbers.

Even if the "if" condition is satisfied, it doesn't evaluate the "then" block.

I think your problem is that your variables don't contain what you think they do. I think your $str1 or $str2 contains something like "taste\n" or so. Check them by printing before your if: print "str1='$str1'\n";.

The trailing newline can be removed with the chomp($str1); function.

juanes
  • 155
  • 1
  • 7
Galimov Albert
  • 7,269
  • 1
  • 24
  • 50
  • How do I remove trailing newline if present before comparing? – hari Dec 26 '12 at 21:56
  • 1
    @hari use `chomp`. See `perldoc -f chomp`. – squiguy Dec 26 '12 at 21:56
  • yes, $str1 has "\n" at the end but using chomp is not helping. If I do: if( $str1 eq "taste\n") it works. – hari Dec 26 '12 at 22:25
  • 2
    Damn, I also had a carriage return. This is what I needed: $str1 =~ s/\r|\n//g; What a rookie mistake. – hari Dec 26 '12 at 23:00
  • @hari: This is a bit late, but if you have a `\r` character in your string I have to wonder how it got there. Windows uses `'\r\n` to mark the end of a line, but if you read from a text file *in text mode* you'll just get the `'\n`. Perhaps you're reading a Windows-formatted text file on a Unix or Cygwin system? – Keith Thompson Jun 15 '16 at 19:02
35

== does a numeric comparison: it converts both arguments to a number and then compares them. As long as $str1 and $str2 both evaluate to 0 as numbers, the condition will be satisfied.

eq does a string comparison: the two arguments must match lexically (case-sensitive) for the condition to be satisfied.

"foo" == "bar";   # True, both strings evaluate to 0.
"foo" eq "bar";   # False, the strings are not equivalent.
"Foo" eq "foo";   # False, the F characters are different cases.
"foo" eq "foo";   # True, both strings match exactly.
cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • 8
    "2foo" == "3bar"; #False, the strings evaluates to respectively 2 and 3 – Jean-Francois T. Dec 01 '14 at 09:49
  • 8
    @Jean-FrancoisT. A more interesting example would have been `"2foo" == "2bar"`, which would be true. Your example producing false is unsurprising and doesn't really illustrate the difference between the operators because those strings won't compare as equal with *any* operator. – cdhowie Dec 01 '14 at 16:43
  • 7
    The idea behind the example was to draw the parallel with "foo"=="bar" which is true, while with "2foo"=="3bar" is not – Jean-Francois T. Dec 01 '14 at 17:08
5

Did you try to chomp the $str1 and $str2?

I found a similar issue with using (another) $str1 eq 'Y' and it only went away when I first did:

chomp($str1);
if ($str1 eq 'Y') {
....
}

works after that.

Hope that helps.

ArnonZ
  • 3,822
  • 4
  • 32
  • 42
user4185253
  • 51
  • 1
  • 2
  • 1
    This helped me! :) It's good to know the function chomp does not return the result, but just change the string itself. – CCNA May 25 '16 at 19:28
-6

Maybe the condition you are using is incorrect:

$str1 == "taste" && $str2 == "waste"

The program will enter into THEN part only when both of the stated conditions are true.

You can try with $str1 == "taste" || $str2 == "waste". This will execute the THEN part if anyone of the above conditions are true.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131