in reply to replacing part of a string
I want to repalce "C" in these functions by $z
Replace with a literal '$z':
my @funcs = ( '$A = B * C;', '$P = D+E+C;' ); for my $func (@funcs) { $func =~ s/C/\$z/g; print "func : $func\n"; } __END__ func : $A = B * $z; func : $P = D+E+$z;
Or, replace with the value of a variable $z:
... my $z = 99; for my $func (@funcs) { $func =~ s/C/$z/g; print "func : $func\n"; } __END__ func : $A = B * 99; func : $P = D+E+99;
(Also note that, because $func is an alias to the original value, the values in @funcs would be modified here. If that's not intended, you'd have to make a copy of the value before doing the substitution...)
|
|---|