Using variable in replacement expression

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.
 
T

Tim McDaniel

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;

As a side note: even in a simple test program, it's best to keep the
discipline of
use warnings;
use strict;
and "my"ing all variables.
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.

Print $cnt after the loop. You'll see that it's 2. That means that
the loop iterated only once.

The problem is the "g" modifier. It replaces ALL occurrences the
first time it runs.

"g" in general is a good idea, to prevent an infinite loop if the RHS
happened to contain the LHS (that is, if $replace contained "xy").

This works for me in 5.14.2:

my $s = "ab xy cd xy ef xy gh\n";
my $cnt = 0;
$s =~ s/xy/++$cnt, "Mark$cnt"/ge;
print $s;

and therefore

my $s = "ab xy cd xy ef xy gh\n";
my $cnt = 0;
my $replace = '++$cnt, "Mark$cnt"';
$s =~ s/xy/$replace/gee;
print $s;
 
R

rob

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.



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.



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.
 
R

rob

As a side note: even in a simple test program, it's best to keep the

discipline of

use warnings;

use strict;

and "my"ing all variables.








Print $cnt after the loop. You'll see that it's 2. That means that

the loop iterated only once.



The problem is the "g" modifier. It replaces ALL occurrences the

first time it runs.



"g" in general is a good idea, to prevent an infinite loop if the RHS

happened to contain the LHS (that is, if $replace contained "xy").



This works for me in 5.14.2:



my $s = "ab xy cd xy ef xy gh\n";

my $cnt = 0;

$s =~ s/xy/++$cnt, "Mark$cnt"/ge;

print $s;



and therefore



my $s = "ab xy cd xy ef xy gh\n";

my $cnt = 0;

my $replace = '++$cnt, "Mark$cnt"';

$s =~ s/xy/$replace/gee;

print $s;


Thanks very much. It works now!

Rob V.
 
C

C.DeRykus

As a side note: even in a simple test program, it's best to keep the

discipline of

use warnings;

use strict;

and "my"ing all variables.








Print $cnt after the loop. You'll see that it's 2. That means that

the loop iterated only once.



The problem is the "g" modifier. It replaces ALL occurrences the

first time it runs.



"g" in general is a good idea, to prevent an infinite loop if the RHS

happened to contain the LHS (that is, if $replace contained "xy").



This works for me in 5.14.2:



my $s = "ab xy cd xy ef xy gh\n";

my $cnt = 0;

$s =~ s/xy/++$cnt, "Mark$cnt"/ge;

print $s;



and therefore



my $s = "ab xy cd xy ef xy gh\n";

my $cnt = 0;

my $replace = '++$cnt, "Mark$cnt"';

$s =~ s/xy/$replace/gee;

print $s;

A variant sans comma operator:

$cnt = 1;
$replace = '"Mark" . $cnt++';
$s =~ s/xy/$replace/gee;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top