You don't actually need two output files here. You can eliminate a rather large step by reading in files 1,2,3 into hashes and then doing the matching directly on the hashes. You will save a lot of time because you will eliminate 1 whole file write and 1 whole file read.

The solution is not very different from what you learned in Compare 2 files and create a new one if it matches. There you had two input files that you wanted to merge into one and you learned you could do it by reading the files into hashes and matching them in memory. Now you have three.

++ for trying to apply what you learned. The only mistake you made was thinking that you needed one output file for every two files, but you don't. The key thing for merging the files is the hashes. Here's pseudo-code for comparing three hashes (rather than two):

#%hFirst stores lines loaded in from first file #%hSecond stores lines loaded in from second file #%hThird stores lines loaded in from third file #OUT is a file handle to your one and only output file while (my ($sKey, $fld1) = each(%hFirst)) { my $fld1 = $hFirst{$sKey}; my $fld2 = exists $hSecond{$sKey} ? $hSecond{$sKey} : "NOT DEFINED"; my $fld3 = exists $hThird{$sKey} ? $hThird{$sKey} : "NOT DEFINED"; print OUT "$fld1======$fld2======$fld3\n"; }

Best, beth


In reply to Re: Merge 3 files into one file by ELISHEVA
in thread Merge 3 files into one file by shawshankred

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.