in reply to Better Way of Manipulating Array Elements

print join ", ", map { chomp; s/(d)/new($1)/; $_ } @a;

Replies are listed 'Best First'.
Re^2: Better Way of Manipulating Array Elements
by ikegami (Patriarch) on May 19, 2009 at 22:48 UTC

    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)/'
      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".

Re^2: Better Way of Manipulating Array Elements
by ikegami (Patriarch) on May 20, 2009 at 00:10 UTC
    Alternate fix:
    use List::MoreUtils qw( apply ); print join ", ", apply { chomp; s/(d)/new($1)/ } @a;
Re^2: Better Way of Manipulating Array Elements
by bichonfrise74 (Vicar) on May 20, 2009 at 00:07 UTC
    Thanks!!! That did the trick.