James wrote:
On Feb 10, 2004, at 5:04 AM, Jan Eden wrote:
Now I found a nicer solution in "Learning Perl Objects, References & Modules" and adapted it:
sub contains { my $contained = shift; my $result = grep { $contained eq $_ } @_; }
Again, no need for the variable.
sub contains { my $contained = shift; return grep $_ eq $contained, @_; }
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...
What do you mean here James? All I can see is that you changed
grep BLOCK
into
grep EXPRESSION
reversed the operands of 'eq' and dropped the assignment.
Am I missing something?
Yes, you did. :D
I was talking about the fix to Jan's version, though I probably should have made that more clear. It does not need to walk the whole list. Here it is again:
sub contains { my $contained = shift; foreach (@_) { return 1 if $_ eq $contained; } return 0; }
I said I cannot fix the grep() version. I'm just not that cool. <laughs>
James
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>