in reply to Fast Replacement
The call to index returns the position of the found character, so you can replace it directly using substr if you capture the output from index. Additionally, as pointed out by muba, you do not need to start from the beginning every time, but save time by starting from the last position found.
use strict; use warnings; my $group = "a!" x 50001; my $count = 0; my $pos = 0; substr $group, $pos, 1, "\n" while( ($pos = index( $group, "!", $pos ) +) > -1 and $count++ < 50000 ); print $group;
|
|---|