in reply to Localize $_ in grep
That is, put the variable you are substituted as the last item in the block. Otherwise, you return the return of the substitute, which is usually 1 or the number of matches (i always forget :/).my @m_h = grep {local($_);s/tw/tr/g; /^t.*/ig;$_} @foo;
In your case, you don't want grep - you want map (actually, you do want grep ... see 2nd update below):
Notice that you don't need local.my @m_h = map {s/tw/tr/g;$_} @foo;
UPDATE:
Oops, this still changes the contents of @foo ... hmmm, how
about a simple copy?
Should be all you need. No?my @m_h = @foo; s/tw/tr/g for @m_h;
UPDATE 2:
After looking at ctilmes reply, how about
this:
Hope this helps. :)my @m_h = grep /^t/i, @foo; s/tw/tr/g for @m_h;
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Localize $_ in grep
by Anonymous Monk on Apr 19, 2012 at 04:00 UTC |