jdagius has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to use grep() to locate items in a list (yes, I know about List::Member, but it won't find 0 (zero) in a list, (bug?).
I wrote a function called member() below which returns 1-based location or 0 if not found. It works fine the first time I call it, but fails subsequently. What's going on?
Tnx,
Johanus
sub member { my $item = shift; my @found = grep $_[$_] eq "$item", 0..$#_; if (defined @found) { return $found[0] + 1; } else { return 0; } } ## end member() @x=(0,1,3); print member(2,@x),"\n"; # not in list print member(0,@x),"\n"; # 1st item in list print member(2,@x),"\n"; # not in list, again
0 1 1 # fails, should return 0, again
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: sub fails second time called
by kyle (Abbot) on Apr 23, 2008 at 15:02 UTC | |
|
Re: sub fails second time called
by Limbic~Region (Chancellor) on Apr 23, 2008 at 15:48 UTC | |
|
Re: sub fails second time called
by toolic (Bishop) on Apr 23, 2008 at 15:50 UTC | |
|
Re: sub fails second time called
by ysth (Canon) on Apr 23, 2008 at 19:08 UTC | |
by jdagius (Initiate) on Apr 24, 2008 at 19:54 UTC |