in reply to Find and replace from two files

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:

  1. 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/)
  2. 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.

  3. The logic of the two loops is still wrong. You need to search the file “template.properties” for a match, and then, when you’ve finished searching (either because you found a match, or because you read through to the end of the file), output the match (if found) or the original (if no match was found).

Lastly, let me highlight some of the Perl best practices you will see in 2teez’s code:

Hope that helps,

Athanasius <°(((><contra mundum

Replies are listed 'Best First'.
Re^2: Find and replace from two files
by jimmydean (Initiate) on Aug 18, 2012 at 15:39 UTC
    2teez & Athanasius,

    Thank you so much for you help! I can't wait to try this out on Monday when I get back to work. I was wondering why people were using the
    use strict; use warnings;
    then Anonymous Monk posted some very helpful links. I hope to be able to help out others like you have helped me.

    Thank you all!

    J