Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: check if 2 values are equal

by thedoe (Monk)
on Jan 24, 2006 at 19:24 UTC ( [id://525294]=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

The problem with this is that the numeric comparison could return false positives. Try doing a comparison of 'aa' == 'bb'. With -w on, this does issue warnings, however it will still return true.

That is why something like Fletcher's looks_like_number from Scalar::Util is needed.

I have not used it myself, so I can not attest to the accuracy of this function, however the code would be like this:

sub equals { my ($val1,$val2) = @_; return 1 if !defined $val1 && !defined $val2; if (looks_like_number($val1) && looks_like_number($val2)) { return $val1 == $val2; } else { return $val1 eq $val2; } }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-04-19 20:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found