zow has asked for the wisdom of the Perl Monks concerning the following question:
I was recently trying to pass the $1 name to a search and replace as part of the value of another variable.
Example:
$search = "(.*)\d\d\d\d\d\d.txt";
# $search should match to some_string200912.txt
$replace = "some_value_$1";
$to_modify =~ s/$replace/$search/g;
If I do:
print $1; # This contains the characters captured in the parantheses (some_string)
But the value of $to_modify is just "some_value_" where I would be hoping for "some_value_some_string"
The search and replace strings are being fed to the script through a delimited config file that is used to assign variables.
I worked around it but found it peculiar. Right now I'm thinking that the value of $1 in the variable value ($replace) is that at the time in which the variable was initialized and thus was empty and that THAT $1 does not get re-initialized to the value in the parantheses at the time that the regex search and replace is run. Does that sound right or is there something else going on?
Thanks for any feedback. BTW using perl 5.8.8 on HP-UX 11.1
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Passing $1 as part of a variable value to a regex search and replace
by johngg (Canon) on Dec 05, 2009 at 20:01 UTC | |
by zow (Sexton) on Dec 05, 2009 at 23:17 UTC | |
|
Re: Passing $1 as part of a variable value to a regex search and replace
by AnomalousMonk (Archbishop) on Dec 05, 2009 at 23:32 UTC | |
|
Re: Passing $1 as part of a variable value to a regex search and replace
by Marshall (Canon) on Dec 06, 2009 at 17:55 UTC |