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[$_]); }
In reply to Re: grep return the index instead of the element content, and its speed
by ikegami
in thread grep return the index instead of the element content, and its speed
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |