Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

using map and swap (aka substitute)

by DamianKaelGreen (Acolyte)
on Jan 14, 2013 at 20:18 UTC ( #1013266=perlmeditation: print w/ replies, xml ) Need Help??

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

Comment on using map and swap (aka substitute)
Select or Download Code
Re: using map and swap (aka substitute)
by blue_cowdawg (Prior) 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
Re: using map and swap (aka substitute)
by BrowserUk (Pope) 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 (Monsignor) 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 AnomalousMonk (Monsignor) 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'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://1013266]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (11)
As of 2014-07-22 12:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (113 votes), past polls