Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Backsubstitution in a variable? 1

Status
Not open for further replies.

JonnoA

Programmer
Jan 19, 2004
7
0
0
GB
I am doing a find-replace on filenames which are 2 numbers then description. I need to transform the filenames. I would like the 2 parts of the substitution as variables (so that I can pass in on the command line). I show a cutdown version here. This works:

my $source = "(\\d\\d)(\.\*)" ;
$_ = "35 example filename.fmn";
s/$source/data $1/; # source substitution works
print("$_\n"); #prints 'data 35' correctly

... but this does NOT work:

my $source = "(\\d\\d)(\.\*)" ;
my $dest = "data $1";
$_ = "35 example filename.fmn";
s/$source/$dest/; # dest substitution does NOT work
print("$_\n"); #prints 'data'

It presumably does not work because $1 is substituted before the s///
I would be very grateful if someone could tell me how I can write the 'dest' variable so that it will work.
Thank you.
Jon
 
$1 doesn't necessarily have a value when assigned to $dest

try $dest='data $1'; #single quotes

that way $1 shouldn't be interpolated before its use in the substitution

HTH
--Paul


It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Unfortunately that doesn't work either. In the example shown, the output is

data $1

The output should be

data 35
 
Anyone else?
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
I tried changing the $dest string with no luck. Do you absolutely need to use a substitution? Using just the example you gave (and having no idea what context this is going to be used in) something like the following would give you the output you're looking for.

Code:
my $source = '(\d{2})(.*)';
my $dest = "data ";
$_ = "35 example filename.fmn";
m/$source/;
$dest .= $1;        # $dest is now the string 'data ' with 
print("$dest\n");   # $1 from the match on the end.
 
You are doing the substitution a little too early :)
It only happens in-place between quotes when the variable is set. Therefore this should work.

my $source = "(\\d\\d)(\.\*)" ;
my $dest = "data";
$_ = "35 example filename.fmn";
s/$source/"$dest $1"/; # this work....
print("$_\n"); #prints 'data 35'

Cheers
 
The substitution is actually inside a for loop which searches through folders and returns the filename. I wanted to pass in the original(source) and new(dest) regular expressions as strings on the command line (using ARGV[1] etc). (the original filename 'format' isn't the same in each folder, so the files in each folder are processed as a separate case).

Seems like it's not possible to do it this way though!

Thanks for your replies
Jon
 
you can also play with the /e modifier....
Code:
my $source = "(\\d\\d)(\.\*)" ;
my $dest = '"data $1"';
$_ = "35 example filename.fmn";
s/$source/$dest/ee; # print also data 35
print("$_\n"); #prints 'data'
 
Would you look at that! Two e's for modifiers? I tried it with one and had no luck. Looks like the second one does the trick.
 
why use a regex? I've always had my arse kicked for this! Unnecessary (it's too early to be spelling words like this...)

I am doing a find-replace on filenames which are [red]2 numbers then description[/red].

[red][tt]substr[/tt][/red]


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top