Forum Moderators: phranque
$string = 'first there is the foo, then comes the bar';
$x = 1;
while ($string =~ m{(
foo |
bar
)}xg) {
print "$1 => lorem\n";
$string =~ s/$1/lorem/xg;
print "$x\n";
$x++;
}
print $string;
# Result:
# foo => lorem
# 1
# bar => lorem
# 2
# first there is the lorem, then comes the lorem while ($string =~ s{(
foo |
bar
)}
{lorem}xg) {
print "$1 => lorem\n";
print "$x\n";
$x++;
}
# Result:
# bar => lorem
# 1
# first there is the lorem, then comes the lorem to make the second version run the success code after each successful substitutionIsn’t that what subroutines or external functions are for? (Actual structure and terminology depending, of course, on language.)