R
RobV
I'm trying to do a global replace with a varying replacement string which includes a counter to count the replacements.
The idea is this (replacing all "xy" strings):
Source: "ab xy cd xy ef xy gh"
Result: "ab Mark1 cd Mark2 ef Mark3 gh"
I read through perlre and the best I can come up with is below. Yet it doesn't do what I wanted, the replacement is identical every time:
$cnt = 1;
$replace = '"Mark$cnt"';
$s = "ab xy cd xy ef xy gh";
while ($s =~ s/xy/$replace/gee) {
$cnt++;
}
print $s;
Result is: "ab Mark1 cd Mark1 ef Mark1 gh"
My understanding of the /ee modifier is that it forces the right-hand side to be evaluated every time, but somewhere in my understanding something seems to be missing.
I'd be grateful if someone could point me the way.
Thanks in advance,
Rob V.
The idea is this (replacing all "xy" strings):
Source: "ab xy cd xy ef xy gh"
Result: "ab Mark1 cd Mark2 ef Mark3 gh"
I read through perlre and the best I can come up with is below. Yet it doesn't do what I wanted, the replacement is identical every time:
$cnt = 1;
$replace = '"Mark$cnt"';
$s = "ab xy cd xy ef xy gh";
while ($s =~ s/xy/$replace/gee) {
$cnt++;
}
print $s;
Result is: "ab Mark1 cd Mark1 ef Mark1 gh"
My understanding of the /ee modifier is that it forces the right-hand side to be evaluated every time, but somewhere in my understanding something seems to be missing.
I'd be grateful if someone could point me the way.
Thanks in advance,
Rob V.