in reply to Re: Better Way of Manipulating Array Elements
in thread Better Way of Manipulating Array Elements

Since it appears to be a complete program, the following would do:

perl -pe'chomp; s/(d)/new($1)/; s/^/,/ if $.-1'

I first considered the following, but it leaves a trailing comma and no LF at file's end:

perl -pl54e's/(d)/new($1)/'

Replies are listed 'Best First'.
Re^3: Better Way of Manipulating Array Elements
by morgon (Priest) on May 20, 2009 at 00:02 UTC
    You are missing that he want's a blanks after the commas. And I improve (golfwise) to:

    perl -pe's/(d)/new($1)/; s/\n/, / unless eof'

    But since capturing a literal regex is silly anyway, the best I can think of at the moment is

    perl -pe's/d/new(d)/;s/\n/, / unless eof'

      You are missing that he want's a blanks after the commas.

      That's not what he said ("join all the elements by a comma"), and that's not what he's currently doing (print join ",", @z;). Trivial change either way.

      I improve (golfwise) to [...] unless eof

      Good, but
      if !eof is even shorter (6 from 10) and clearer.
      if !eof() means something different and might be desirable.

      But since capturing a literal regex is silly

      I doubt the OP is really matching "d".

        print join ", ", map { chomp; s/(d)/new(\1)/ } @a;
        I was just looking at that - but you are right about the blanks - 1:0 for you.

        if !eof is shorter and clearer.
        Damn it you are right again - 2:0 for you.

        *defeated* :-)

        if !eof() means something different and might be desirable
        My previous post was not really meant to be taken seriously - so it's even better that I learn something here - many thanks!