There's more than one way to do things PerlMonks

### Re^2: check if 2 values are equal

by clinton (Priest)
 on Jan 24, 2006 at 19:31 UTC ( #525296=note: print w/replies, xml ) Need Help??

in reply to Re: check if 2 values are equal
in thread check if 2 values are equal

I think you can do away with the third condition of (\$val1 == \$val2).

First, the eq should catch differences between numbers equally well as the ==. See Comparing values.

Secondly, the == would fail (under strict) if one of the values were a string.

For eq to work for numbers, you need to know that the values are being represented internally as numbers, for instance:

```  \$a = 1000;
\$b = 1e3;
print \$a eq \$b ? 1 : 0;
> 1
\$b = "1e3";
print \$a eq \$b ? 1 : 0;
> 0
\$b=\$b+0;
print \$a eq \$b ? 1 : 0;
> 1

Replies are listed 'Best First'.
Re^3: check if 2 values are equal
by Corion (Pope) on Jan 24, 2006 at 19:35 UTC

My solution is wrong, but for different reasons. eq cannot replace ==:

```\$a = "01000";
\$b = 1000;

printf "\\$a eq \\$b : %s\n", \$a eq \$b;
printf "\\$a == \\$b : %s\n", \$a == \$b;

use strict; has no bearing on comparisons. What you are thinking of is use warnings;, which will warn about non-numeric values being compared with ==.

Re use warnings rather than use strict - thanks for the correction.

Likewise, you are right about the "0100" not being equal to 100, which is what I meant about being sure that the values are represented internally as numbers.

So if you said:

``` \$a = "01000";
\$b = 1000;
print \$a eq \$b ? 1 : 0;
> 0
\$a=\$a+0;
print \$a eq \$b ? 1 : 0;
> 1
Re^3: check if 2 values are equal
by Happy-the-monk (Abbot) on Jan 24, 2006 at 19:38 UTC

I think you can do away with the third condition of (\$val1 == \$val2).

Tricky one. I thought the same at first.
But think of 0 and 0.0 or 0E0 - all numerically equal, but not sting equal.
Same goes for 1.1 and 1.10 and of course, there's more to think about.

Cheers, Sören

Create A New User
Node Status?
node history
Node Type: note [id://525296]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2018-01-21 17:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How did you see in the new year?

Results (228 votes). Check out past polls.

Notices?