Hi,
I'm processing a few hundred text files every night to do some cleanup. Basically, I loop on the files and process every line...easy. The thing is, I want to read the regular expressions from a configuration file. So I basicaly load the regex into a @regex array and I loop this array. It all works fine, except when I want to use $1.
I've created a sample that produces the same as my script (see below). This is the input:
This is line 1
This is line 21
I am expecting this:
This is line one
This is dummy val: 21
but I get this (notice the $1):
This is line one
This is dummy val: $1
This is the script:
[tt]
my @values = ( 'This is line 1',
'This is line 21',
);
# Initialise regex array (normaly read from file)
my @regex = ( [ qr/line 1/i , 'line one' ],
[ qr/line ([0-9]+)/i , 'dummy val: $1' ],
);
# Print out before running
print "Data before processing:\n\t", join("\n\t",@values),"\n";
# Loop on all lines...
foreach my $line (@values) {
# Looping on all regex
foreach (@regex) { $line =~ s/$$_[0]/$$_[1]/ }
}
# Print out before running
print "Data after processing:\n\t", join("\n\t",@values),"\n";
[/tt]
I must be missing a small thing here...Any suggestions?
AD AUGUSTA PER ANGUSTA
Thierry
I'm processing a few hundred text files every night to do some cleanup. Basically, I loop on the files and process every line...easy. The thing is, I want to read the regular expressions from a configuration file. So I basicaly load the regex into a @regex array and I loop this array. It all works fine, except when I want to use $1.
I've created a sample that produces the same as my script (see below). This is the input:
This is line 1
This is line 21
I am expecting this:
This is line one
This is dummy val: 21
but I get this (notice the $1):
This is line one
This is dummy val: $1
This is the script:
[tt]
my @values = ( 'This is line 1',
'This is line 21',
);
# Initialise regex array (normaly read from file)
my @regex = ( [ qr/line 1/i , 'line one' ],
[ qr/line ([0-9]+)/i , 'dummy val: $1' ],
);
# Print out before running
print "Data before processing:\n\t", join("\n\t",@values),"\n";
# Loop on all lines...
foreach my $line (@values) {
# Looping on all regex
foreach (@regex) { $line =~ s/$$_[0]/$$_[1]/ }
}
# Print out before running
print "Data after processing:\n\t", join("\n\t",@values),"\n";
[/tt]
I must be missing a small thing here...Any suggestions?
AD AUGUSTA PER ANGUSTA
Thierry