It's my understanding that back references (\1) are a bit slow. What follows are solutions that don't use back references. You'll have to benchmark them to see if they're faster.
#my @chars = grep !$seen{$_}++, $text =~ /./g; my @chars = 'a'..'z'; my ($re) = map qr/$_/, join '|', map "(?<=${_}{3})$_+", map quotemeta, @chars; $text =~ s/$re//g;
#my @chars = grep !$seen{$_}++, $text =~ /./g; my @chars = 'a'..'z'; my ($re) = map qr/$_/, join '|', map "${_}{4,}", map quotemeta, @chars; $text =~ s/($re)/substr($1,0,3)/eg;
#my @chars = grep !$seen{$_}++, $text =~ /./g; my @chars = 'a'..'z'; my ($re) = map qr/$_/, join '|', map "${_}{4,}", map quotemeta, @chars; $text =~ s/$re/substr($text,$-[0],3)/eg;
Update: Fixed bug identified in reply.
In reply to Re: More efficient way to truncate long strings of the same character
by ikegami
in thread More efficient way to truncate long strings of the same character
by kurtis
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |