Click to See Complete Forum and Search --> : Finding the INDICES of multiple occurences of an WORD in an ARRAY


kimskams80
03-16-2009, 07:39 AM
Hi

I want to know how can I find ALL indices of all occurrences of a word in an ARRAY...

For example, I have a string which is repeated in a file (read in array linewise) .... how can I find all the line numbers where string exists...

Thanks in Adv

Sixtease
03-17-2009, 03:11 AM
The List::MoreUtils (http://search.cpan.org/~vparseval/List-MoreUtils-0.22/lib/List/MoreUtils.pm) module has an indexes function. Works just like grep but returns the indices instead of the elements.

If you don't want to install a module for this, then you can do something like
grep {$array[$_] =~ /regexp/} 0 .. $#array