$val = 1; while ($val < 5) { $foo = "some sentence x"; # Move this here $foo =~ s/x/$val/; print "$val $foo\n"; $val++; }