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:

sub member(&@$) { my ($eq, $array, $item) = @_; for (@$array) { return 1 if $eq->($_, $item) } return undef; }

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) == $TRUE
which should really test for truth, so it should be
... if (($_ eq $item) != $FALSE) eq $TRUE
etc.


In reply to Re: Is list member by ariels
in thread Is list member by hotshot

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.