XP is just a number | |
PerlMonks |
Re: Is list memberby ariels (Curate) |
on May 06, 2002 at 08:15 UTC ( [id://164244]=note: print w/replies, xml ) | Need Help?? |
No, you cannot write a single function that will do both. You must have some notion of what "equality" means before you can write this function. Say we denote such equality (in English, not in Perl) with the symbol `~'. You'll need to decide e.g.
It's also not clear from your question if you want the same function to work for finding both numbers in a number list and strings in a string list, or if you'd also like it magically to "do the right thing" when searching for a string in a number list etc. Consider what you want to do, and then replace your problematic comparison e.g. with one of these:
For an all-singing, all-dancing run-time customizable (and slower) solution, pass in a function to perform the comparison:
Finally, why are you trying to circumvent Perl's booleans? The use of things like $TRUE and $FALSE is a bad idea. First, builtins might not return these values, so you end up with 4 different values, and cannot do stuff like "return $a == $b" in the routine you pass to member. The values of TRUE and FALSE aren't going to change in the lifetime of your program; if they do, all your code will fall apart, so it doesn't matter. And note you're still using Perl's booleans, not yours: ... if ($_ eq $item)should really test for truth, so it should be ... if ($_ eq $item) == $TRUEwhich should really test for truth, so it should be ... if (($_ eq $item) != $FALSE) eq $TRUEetc.
In Section
Seekers of Perl Wisdom
|
|