Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I easly get the index of a grep?

Status
Not open for further replies.

Tve

Programmer
May 22, 2000
166
FR

Hi,

If I do a grep on an array, grep returns the lines where the regex was found.

I would like to return the line numbers instead of the contents. I've seen a standard solution (?somewhere?), but I can't seem to find it back.

I'm trying not to re-invent the wheel, so if somebody has this solution, it would really be appreciated. AD AUGUSTA PER ANGUSTA

Thierry
 
This is probably the one you are looking for
Code:
@results = grep { $array[$_] =~ /pattern/ } (0..$#array)
and this is probably not it
Code:
my @results =
        map { $_->[1] }
        grep { $_->[0] =~ /pattern/ }
        map { [ $_, $i++] }
        @array;
it's always good to have options, though.

jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top