Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Regex to replace consecutive tokens

by hdb (Monsignor)
on Oct 18, 2013 at 13:24 UTC ( [id://1058756]=note: print w/replies, xml ) Need Help??


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/,//;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1058756]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-19 06:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found