in reply to grep return the index instead of the element content, and its speed
Instead of
my @filtered = grep { condition($_) } @elements;
I think you're asking for
my @indexes = grep { condition($elements[$_]) } 0..$#elements;
I don't think you'll get much speed out of that unless you're dealing with very long strings.
Update: You could also use one of the following. They use much less memory and they're probably faster.
my @filtered; for (@elements) { # Not any list, an array specifically. push @filtered, $_ if condition($_); }
or
my @indexes; for (0..$#elements) { push @indexes, $_ if condition($elements[$_]); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: grep return the index instead of the element content, and its speed
by Anonymous Monk on Feb 28, 2008 at 09:51 UTC | |
by ikegami (Patriarch) on Feb 28, 2008 at 10:30 UTC |