in reply to processing a file once within a nest for loop
The easiest way to patch this to make it work is to copy the loop variable into another temporary variable before doing the substitution:
Some people don't like temporary variables, especially in cases like this, where it looks like you could remove the variable with no ill effect to the code (but you can't). But, since this is perl, there are other ways to do it. One would be to match $j and then do the replacement in the print statement instead of using s///:# Warning, untested code below. for my $j(@templ) { my $k = $j; $k =~ s/^(\w+)\@DOMAIN$/$1\@$i/; print FH $k; } # Or: foreach (@templ) { my $j = $_; $j =~ s/^(\w+)\@DOMAIN$/$1\@$i/; print FH $j; }
I hope these help.# Warning, untested code below. for (@templ) { /^(\w+)\@DOMAIN$/ and print FH "$1\@$i" or print FH; # implicitly prints $_ as usual } # Or if you prefer "if": for (@templ) { if (/^(\w+)\@DOMAIN$/) { print FH "$1\@$i"; } else { print FH; } }
Alan
|
|---|