Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: check if 2 values are equal

by GrandFather (Saint)
on Jan 24, 2006 at 20:45 UTC ( [id://525309]=note: print w/replies, xml ) Need Help??


in reply to check if 2 values are equal

The following code may do what you want:

use strict; use warnings; my @pairs = ( [undef, undef], [0, undef], [0, 0], ['', undef], ['', ''], [1, und +ef], [1, 1], [0, '0'], [0, 0], ['0', '0'], ['0e0', '0'], ['x', '0'], ['x', 0], ); for (@pairs) { my @pair = (@$_); print defined $pair[0] ? ">$pair[0]<" : 'undef'; print ', ' . (defined $pair[1] ? ">$pair[1]<" : 'undef'); print ': ' . (equals (@$_) ? "match\n" : "different\n"); } sub equals { my ($lhs, $rhs) = @_; #False if mix of defined and undef return 0 if defined $lhs != defined $rhs; # True if both undef return 1 if ! defined $lhs && ! defined $rhs; # False if different as numbers no warnings "numeric"; return 0 if (0 + $lhs) != (0 + $rhs); # False if different as strings return 0 if ('' . $lhs) ne ('' . $rhs); return 1; }

Prints:

undef, undef: match >0<, undef: different >0<, >0<: match ><, undef: different ><, ><: match >1<, undef: different >1<, >1<: match >0<, >0<: match >0<, >0<: match >0<, >0<: match >0e0<, >0<: different >x<, >0<: different >x<, >0<: different

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: check if 2 values are equal
by eXile (Priest) on Jan 24, 2006 at 21:18 UTC
    thanks very much, this does what i'd like it to do, and if i compare it to my orginal function it even takes care of the 'numeric 0' vs. 'empty string' inequality.
    my @vals = (undef, 0, '0', 'aap', 1, 't',''); my @expl = ('undef','numeric 0','string 0', 'string "aap"', 'nu +mber 1', 'string t','empty string'); my ($i,$j); for ($i=0; $i <= scalar(@vals-1); $i++) { for ($j=0; $j <= scalar(@vals-1); $j++) { # equals 1 print "equals_org '$expl[$i]'\t'$expl[$j]'\t" . equals_org($va +ls[$i],$vals[$j]) . "\n"; print "equals_new '$expl[$i]'\t'$expl[$j]'\t" . equals_new($va +ls[$i],$vals[$j]) . "\n"; print "--\n"; } } sub equals_org { my ($val1, $val2) = @_; # first test for undef-ness if ( ! defined $val1 || ! defined $val2 ) { return 1 if (! defined $val1 && ! defined $val2 ); return undef; #case one is undef, other is def } # have to do next line this because strings evaluate to 0 in == co +ntext ... if ( ! $val1 && ! $val2 ) { # then test for ""-ness return 1 if ( ($val1 eq "") && ($val2 eq "") ); return undef if ( $val1 eq "" || $val1 eq "" ); #case one is "" other is 0 # then test for 0-ness return 1 if ( ($val1 == 0) && ($val2 == 0) ); } # then test for equality using 'eq' return 1 if ( $val1 eq $val2 ); # then test for equality using '==' if ( int($val1) && int($val2) ) { # all other number cases are already covered by # above tests (eg. both 0, or both the same string) # this merely takes care of cases like: # 1000.0000 == 1000 return 1 if ( $val1 == $val2 ); } # couldn't find any, so i guess they are not equal return undef; } sub equals_new { my ($lhs, $rhs) = @_; #False if mix of defined and undef return 0 if defined $lhs != defined $rhs; # True if both undef return 1 if ! defined $lhs && ! defined $rhs; # False if different as numbers no warnings "numeric"; return 0 if (0 + $lhs) != (0 + $rhs); # False if different as strings return 0 if ('' . $lhs) ne ('' . $rhs); return 1; }
Re^2: check if 2 values are equal
by thedoe (Monk) on Jan 24, 2006 at 21:57 UTC

    I'm a bit confused. Shouldn't

    >0e0<, >0<: different
    be a match?

      0e0 and 0 are different as a string and the same as a number. OP seems to like it evaluated as different. A solution involving looks_like_number is required to get the result you would like, else 'x' and 'y' would be a match (0 == 0).


      DWIM is Perl's answer to Gödel

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-26 09:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found