Hello jimmydean, and welcome to the Monastery!
First, ++2teez for the answer using a hash. That is the correct way to go.
However, I thought you might like some feedback on why your code isn’t working:
If you had included use warnings at the head of your script, you would have seen:
Name "main::v" used only once: possible typo at ... line ... Use of uninitialized value $_ in pattern match (m//) at ... line ..., +<templateFile> line 1. street.addressstreet.address=#enter address# city.name=place state.nam +e=unknown
The second warning shows what is wrong: Because you are using a named variable $tmpLine in the inner foreach loop, the default variable $_ is uninitialized. Change the regex-matching line to:
if ($tmpLine =~ /$k/)
The inner foreach loop reads to the end of the file “template.properties”, so on the next iteration of the outer loop you need to reset the filehandle’s position to the beginning of the file:
foreach $line (<currentValues>) { ($k, $v) = split /=/, $line; seek(templateFile, 0, 0); # <== ADD THIS LINE foreach $tmpLine(<templateFile>) ...
See seek.
Lastly, let me highlight some of the Perl best practices you will see in 2teez’s code:
Always begin a script with:
use strict; use warnings;
Prefer lexical filehandles:
open(my $templateFile...)
Prefer the 3-argument form of open:
open(my $currentValues, '<', 'current.txt') or die ...
Hope that helps,
Athanasius <°(((>< contra mundum
In reply to Re: Find and replace from two files
by Athanasius
in thread Find and replace from two files
by jimmydean
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |