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


In reply to Re: Find and replace from two files by Athanasius
in thread Find and replace from two files by jimmydean

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.