my $x = "<0>"; my $pos = 0; for (qw {<1> <2> <3>}) { pos($x) = $pos; $x =~ s/\G(<\d+>)/$1$_/; $pos += length $1; } say $x; __END__ <0><1><2><3>