Right, that's a point, but the way I look at it it would still work even if s/// was returning the changed string (except, of course, when the returned string was something like zero or an empty string).
Really? Then what should s/// return if *no* substitutions took place? undef? That would make
@new_list = map {s/foo/bar/} @old_list;
fail to do the right thing most of the time as well - it would only work if the old list only contain strings that would be affected by s///. You would still need to write that as:
@new_list = map {s/foo/bar/; $_} @old_list;
most of the time.
Yes, you're right. That was my first thought about how it should (or "should") work, and it was clearly ill-considered. My second thought was a little better (or so I think): s/// could have been set-up to return the count of changes in scalar context, and the string itself in list context -- though that's perhaps a bit of an abuse of the idea of "list" context, because you're never going to have more than the one string returned from a s/// (unlike a m///, which can have multiple captures). Also it would create it's own "DWIM" problem in another area... for example, while this would work:
@transformed = map {s/$old/$new/} @raw;
This would not work:
$_ = $raw; $transformed = s/$old/$new/;

Anyway, the perl 6 solution to these problems is add more contexts. If I understand correctly, in logical context s/// will return whether the change happened, in numeric context you'll get the count, and in string context it'll return the matched string.

So whether or not Larry Wall would agree that he "blew it" originally, he's going to fix the problem.


In reply to Re^4: A hint about using map by doom
in thread Turning foreach into map? by ghenry

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.