Anytime you substitute inside of a grep or map block, you have to do this:
my @m_h = grep {local($_);s/tw/tr/g; /^t.*/ig;$_} @foo;
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 :/).

In your case, you don't want grep - you want map (actually, you do want grep ... see 2nd update below):

my @m_h = map {s/tw/tr/g;$_} @foo;
Notice that you don't need local.

UPDATE:
Oops, this still changes the contents of @foo ... hmmm, how about a simple copy?

my @m_h = @foo; s/tw/tr/g for @m_h;
Should be all you need. No?

UPDATE 2:
After looking at ctilmes reply, how about this:

my @m_h = grep /^t/i, @foo; s/tw/tr/g for @m_h;

Hope this helps. :)

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)

In reply to Re: Localize $_ in grep by jeffa
in thread Localize $_ in grep by InfiniteSilence

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.