James Edward Gray II wrote:
>That works, though I would probably drop the extra variable.
>
>sub contains {
> my $contained = shift;
> foreach (@_) { return 1 if $_ eq $contained; }
> return 0;
>}
>
Perfect. Returns 1 as soon as it finds the element.
>I'm not Randal, but will I do? <laughs>
>
To my experience, your postings have been highly informative.
>The problem with both of your posted solutions is that they run the
>entire list of elements. The question is, "Is $contained in there
>somewhere?" That means that the first time we see it, we have answered
>that question. If we have 20,000 elements and we find it in the second
>slot, we waste 19,998 lookups, right?
>
>We can't fix this in the grep() version, because grep() finds ALL
>matches, not just the first. It must walk the list to do its job.
>However, I snuck in a fix for your version above. Go take a peak...
Your suggestion is certainly a "peak" in elegant programming. ;)
Thanks! Jan
--
Either this man is dead or my watch has stopped. - Groucho Marx
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>