This, and BrowserUk's suggestion are both good ones. BrowserUk's I wasn't even aware of, and this one I had somewhat forgot about. However, I do like the consistency of having one method of doing it that works both standalone and in map. Although I usually argue that adding a variable at the end of a map block isn't that bad, I'm going to be a devil's advocate and argue against it here.

my @output = map { (my $i = $_) =~ s/foo/bar/; $i } @input;

Seems less readable to me than

my @output = map { $_ \~ s/foo/bar/ } @input;

And I like the use of 'map' instead of 'filter' (especially since I think of grep when I think of filter).

I guess it looks like personal preference here, so maybe I'll just perfect this one and keep it for myself (since I don't use $1 and $2 very often from what I've noticed, I wouldn't have to fix them just for myself).

    -Bryan


In reply to Re^2: A Functional Substitute by mrborisguy
in thread A Functional Substitute by mrborisguy

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.