in reply to Re: Regex to replace consecutive tokens
in thread Regex to replace consecutive tokens

This way you will lose all trailing commas...

$s="1,2,3,,5,6,,,9,10,,,,14,15,,,,,,,,,,,,,,," will turn into $s="1,2,3,0,5,6,0,0,9,10,0,0,0,14,15".

Replies are listed 'Best First'.
Re^3: Regex to replace consecutive tokens
by choroba (Cardinal) on Oct 18, 2013 at 13:44 UTC
    Easy to fix:
    my $t = '1,2,3,,5,6,,,9,10,,,,14,15,,,,,,,,,,,,,,,'; my $s = join ',' => map { $_ ne '' ? $_ : '0' } split /,/ => $t, 1 + $t =~ y/,//;

    Update: Or, maybe a bit more readable

    my $s = join ',' => map $_ || 0, split /,/ => $t, 1 + $t =~ y/,//;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ