How can I also get an element's index when I grep through an array?

Posted by Geo on Stack Overflow See other posts from Stack Overflow or by Geo
Published on 2010-06-10T16:39:02Z Indexed on 2010/06/10 16:42 UTC
Read the original article Hit count: 216

Filed under:
|

Let's say I have this list:

my @list = qw(one two three four five);

and I want to grab all the elements containing o. I'd have this:

my @containing_o = grep { /o/ } @list;

But what would I have to do to also receive an index, or to be able to access the index in grep's body?

© Stack Overflow or respective owner

Related posts about perl

Related posts about grep