http://www.perlmonks.org?node_id=1013266

my @list= qw(first_string second_string); @list = map(s/_string$//,@list); print @list;

Returns a bunch of ones, in this case "11"

What should be done instead:

my @list= qw(first_string second_string); @list = map((s/_string$// and $_), @list); print @list;

Another similar reference can be found at: id:20505

Replies are listed 'Best First'.
Re: using map and swap (aka substitute)
by BrowserUk (Patriarch) on Jan 14, 2013 at 20:24 UTC

    Since you are overwriting the original array with the results, the simplest solution is:

    s/_string$// for @list;

    Which mutates the array in-place, thus avoids constructing two lists (in and out of map) as well as 'curing' the perceived problem.

    And if you wanted to create a new list from the modified values whilst retaining the original array unmodified, I'd probably do it this way also:

    my @list = ...; my @newList = @list; s/_string$// for @newList;

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      my @newList = @list; s/_string$// for @newList

      Or just:

      s/_string$// for my @newList = @list;
Re: using map and swap (aka substitute)
by AnomalousMonk (Archbishop) on Jan 14, 2013 at 22:09 UTC

    With 5.14+, the  /r modifier can be used with  s/// substitution to achieve non-mutating substitution with return of a copy of the string (although the intermediate array creation by map noted by BrowserUk will, I think, still occur). See  s/// in the Regexp Quote-Like Operators section of perlop:

    If the "/r" (non-destructive) option is used [in s///] then it runs the
    substitution on a copy of the string and instead of returning
    the number of substitutions, it returns the copy whether or not
    a substitution occurred. The original string is never changed ...
    >perl -wMstrict -le "my @list= qw(first_string second_string THIRD fourth_string); my @newlist = map s/_string$//r, @list; printf qq{'$_' } for @list; print ''; printf qq{'$_' } for @newlist; " 'first_string' 'second_string' 'THIRD' 'fourth_string' 'first' 'second' 'THIRD' 'fourth'
Re: using map and swap (aka substitute)
by AnomalousMonk (Archbishop) on Jan 14, 2013 at 21:40 UTC
    What should be done instead:

    my @list= qw(first_string second_string); @list = map((s/_string$// and $_), @list); print @list;

    This should be done only if one wishes to reduce all strings on which no substitution is done to empty strings:

    >perl -wMstrict -le "my @list= qw(first_string second_string THIRD fourth_string); @list = map((s/_string$// and $_), @list); printf qq{'$_' } for @list; " 'first' 'second' '' 'fourth'

    If this is not the case, BrowserUk has shown some alternatives.

Re: using map and swap (aka substitute)
by blue_cowdawg (Monsignor) on Jan 14, 2013 at 20:23 UTC

    is there a question in there?


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg