Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: More efficient way to truncate long strings of the same character

by kvale (Monsignor)
on Oct 30, 2008 at 18:22 UTC ( #720534=note: print w/replies, xml ) Need Help??


in reply to More efficient way to truncate long strings of the same character

% perl -e'$x="aaaaaabbcccccd"; $x=~s/(.)\1{2,}/$1$1$1/g; print "$x\n"; +' aaabbcccd

-Mark

  • Comment on Re: More efficient way to truncate long strings of the same character
  • Download Code

Replies are listed 'Best First'.
Re^2: More efficient way to truncate long strings of the same character
by JavaFan (Canon) on Oct 30, 2008 at 18:34 UTC
    That would do needless work if a character is repeated only twice. You only have to modify the string is a character appears four or more times in a row. So, my suggestion is:
    s/(.)\1{3,}/$1$1$1/g;
    And in 5.10, use of the \K operator may be faster (but I haven't checked it)
    s/(.)\1\1\K\1+//g;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://720534]
help
Chatterbox?
[marto]: hey Corion, good weekend?
[Corion]: marto: Yeah, even though I didn't write any code :)
[Corion]: But at least I have a plan of action to move the site to https, played some (free!) VR games with friends and watched the plans for the next German Perl workshop progress ;)
[Corion]: AltSpace VR is amazingly good - highly polished and with some of the games you get for free what you'd pay EUR 20 or EUR 40 otherwise
[Corion]: But maybe it's also due to that I play with friends, which makes a game more enjoyable anyway ;)

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2017-08-21 09:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Who is your favorite scientist and why?



























    Results (319 votes). Check out past polls.

    Notices?