in reply to Replace only selected characters

Yet another way using a global look-ahead match and pos to find the commas then substr to erase all but the first, working back from the end of the string.

knoppix@Microknoppix:~$ perl -E ' > $l = q{abc,def,ghi,jkl,mno}; > say $l; > push @p, pos $l while $l =~ m{(?=,)}g; > substr $l, $_, 1, q{} for reverse @p[ 1 .. $#p ]; > say $l;' abc,def,ghi,jkl,mno abc,defghijklmno knoppix@Microknoppix:~$

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: Replace only selected characters
by hbm (Hermit) on Oct 17, 2011 at 13:37 UTC

    Interesting. I wondered why the look-ahead (and not just /,/g), but now I see.

    Note that you can do this:

    push @p, pos $l while $l =~ m{(?=,)}g;

    As:

    push @p, @- while $l =~ /,/g;
      Thanks everyone..Some very good methods