I have a perl script that tries to merge 3 files into one file. However due to some issues with the algorithm it is currently taking a long time to finish. Need some suggestions/advice from the expert monks here. Any help is much appreciated.

Example of Input and Output files

Inputfile1 === Inputfile 2 === Inputfile3

A1 B1 ====== B3, C3, D3 ====== B4
A2 B2 ====== B7, C7, D7 ====== C5
A3 B3 ====== B10, C10, D10 ====== D1
A4 B4 ====== B1, C1, D1 ====== D20
….
….

Output file should look like this

A1 B1 ====== B1 C1 D1 ====== B1 C1 D1
A2 B2 ====== B2 C2 D2 ====== B2 C2 D2
A3 B3 ====== B3 C3 D3 ====== B3 C3 D3
A4 B4 ====== B4 C4 D4 ====== B4 C4 D4
A5 B5 ====== NOT_FNDinFile2 ====== B5 C5 D5
A6 B6 ====== B6 C6 D6 ====== NOT_FNDinFile3
A7 B7 ====== NOT_FNDinFile2 ====== NOT_FNDinFile3
NOT_FNDinFile1 ====== B8 C8 D8 ====== B8 C8 D8
…… ……

Here is my Code..

First I merge File1 and File2 and create a Output file OUT1
Next I merge the Output file 1 and File3.
open(OUT1, '>', $File4) or die("Can't create output file \"$File4\": $!\n"); open(OUT2, '>', $File5) or die("Can't create output file \"$File5\": $!\n"); %file2_data; { open($fh_keys, '<', $File2) or die("Can't open key file \"$File2\": + $!\n"); while ($line1 = <$fh_keys>) { chomp($line1); $line1 =~ s/^\s+//; # Remove the space from begining of the lin +e $line1 =~ s/\s+$//; # Remove the space from end of the line $first = (split /\t/, $line1)[0]; $file2_data{$first} = $line1; } } close($fh_keys); %file1_data; { open($fh_in, '<', $File1) or die("Can't open input file \"$File1\": $!\n"); while (<$fh_in>) { chomp; $flag = 0; $sec = (split /\t/, $_)[1]; $file1_data{$sec} = $_; if ($file2_data{$sec}) { print OUT1"$_\tFOUND_Data\t$file2_data{$sec}\n"; } else { print OUT1"$_\tNOTFOUND_IN_FILE2\n"; } } } close($fh_in); while ( ($KEY_1, $VALUE_1) = each %file2_data ) { if ($file1_data{$KEY_1}) { print OUT1"$file1_data{$KEY_1}\tFOUND_Data\t$VALUE_1\n"; } else { print OUT1"$VALUE_1\tNOTFOUND_IN_FILE1\n"; } } open my $File3_IN1, q{<}, "$File3" or die qq{Can't open "$File3": $!\n}; my @numbers = (); while (<$File3_IN1>) { chomp; $_ =~ s/^\s+//; $_ =~ s/\s+$//; if ($_ == "B") { push @numbers_B, $_; } els if ($_ == "C") { push @numbers_C, $_; } els if ($_ == "D") { push @numbers_D, $_; } } close($File3_IN1); close(OUT1); my $rxFindB = do{ local $" = q{|}; qr{(@numbers_B)}; }; my $rxFindC = do{ local $" = q{|}; qr{(@numbers_C)}; }; my $rxFindD = do{ local $" = q{|}; qr{(@numbers_D)}; }; open OUT1_IN2,"cat $File4 |" or die "Can't open $File4: $!\n"; while (<OUT1_IN2>) { chomp; if m{$rxFindB} { print OUT2"$_\t$1\t"; } else { print OUT2"$_\tNOT_FOUND_B_InFile3\t"; } if m{$rxFindC} { print OUT2"$2\t"; } else { print OUT2"$_\tNOT_FOUND_C_InFile3\t"; } if m{$rxFindD} { print OUT2"$3\n"; } else { print OUT2"$_\tNOT_FOUND_D_InFile3\t"; } } close(OUT1_IN2);

In reply to 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.