in reply to Merge 3 files into one file

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

Replies are listed 'Best First'.
Re^2: Merge 3 files into one file
by shawshankred (Sexton) on Mar 13, 2009 at 04:53 UTC
    ELISHEVA,
    Thanks a lot, as suggested I used hash and it worked fine and takes less than a minute.