"be consistent" | |
PerlMonks |
Re: check if 2 values are equalby davido (Cardinal) |
on Jan 24, 2006 at 23:37 UTC ( [id://525349]=note: print w/replies, xml ) | Need Help?? |
Perl already knows how to do all the work for you. You can just use a hash in almost the same way you would assure uniqueness in a list.:
This works by creating a hash whos keys are the items being compared. If after creating the keys you have one element, you've got equality. If you have more than one element, you have inequality. The only tricky part is to be sure to silence the warning you get for making a hash key out of 'undef', and to deal with the fact that undef stringifies to ''. Here is a test example:
Update: Another advantage to this method that may not be immediately apparent is that it can be used to test a list of any number of items. Enjoy! Dave
In Section
Seekers of Perl Wisdom
|
|