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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: check if 2 values are equal
by eXile (Priest) on Jan 24, 2006 at 21:18 UTC | |
|
Re^2: check if 2 values are equal
by thedoe (Monk) on Jan 24, 2006 at 21:57 UTC | |
by GrandFather (Saint) on Jan 24, 2006 at 22:30 UTC | |
by thedoe (Monk) on Jan 25, 2006 at 15:54 UTC |