$x = 1; for(1..10){ $x =~ s/((.)\2*)/length($1).$2/ge; #print "@{[split//,$x]}\n"; }