in reply to Re: do $n exist in grep?
in thread do $n exist in grep?
I expanded your code to show relevance to the OP's question.
Your map shows something that took me a long time to figure out,
how to return "nothing" from a map{}!
I think this code is well past what the OP wanted.use strict; use warnings; my @x = ("aaBxyz", "..Bxys", "bbxyzzy", "xxAx"); my @result = map { /^..(A|B)./ ? [ $1, $_ ] : () } @x; # @result is a "2-d array", references to "rows of array" # I swapped $1 and $_ because I thought the print out looked better # Of course every map{} can be expressed as a for loop... foreach my $row_ref (@result) { print "@$row_ref\n"; } =PRINTS: B aaBxyz B ..Bxys A xxAx =cut
ADDED: If the OP wants a subset of something, think GREP. If the OP wants to transform an input into something else, think MAP.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: do $n exist in grep?
by davido (Cardinal) on May 12, 2021 at 16:58 UTC | |
by choroba (Cardinal) on May 12, 2021 at 17:02 UTC | |
by davido (Cardinal) on May 12, 2021 at 17:26 UTC | |
by Marshall (Canon) on May 12, 2021 at 20:46 UTC |