http://qs321.pair.com?node_id=1038886


in reply to Fast Replacement

The reason it is slow is that for every call to index, Perl has to go through the process of checking each character in the string whether it is an "!". Again and again and again. So you could use the $index variable to tell Perl not to bother about the characters it has already checked:

while ( index($group, "!", $index) > -1 and $index<50000 ) {

Alternatively, because TIMTOWTDI:

use strict; use warnings; my $string = "abc!def!ghi!jkl!mno!pqr!stu!vwx!yz"; my $limit = 3; $string = join("\n", split(/!/, $string, $limit)); print $string;