http://www.perlmonks.org?node_id=1041359


in reply to Index number repeating in for loop

...or simply...

print "'F' is at index: $_\n" for grep { $text[$_] eq 'F' } 0 .. $#text;

List::MoreUtils provides a sugary alternative, "indexes":

use List::MoreUtils qw( indexes ); # ... print "'F' is at index: $_\n" for indexes { $_ eq 'F' } @text;

This is a case where I prefer grep because what it's doing isn't unclear or awkward enough to justify the module (imho).


Dave