my $string = 'thisisatest'; for my $i (0 .. length($string)-2) { (my $tmp = $string) =~ s/(.{$i})(.)(.)/$1$3$2/; print $tmp, "\n"; }