Your statement of the problem is a little confusing. You said:
I am trying to compare all of the lines from three files and then print the result to a final file.

But your code and data samples involve only two input files, not three. Next, you said:

If the ids are not the same, I don't want it to write anything to the final file unless one of the ids was blank, but I do want to write them if they are the same, such as:

But you show an example for "Final" output that has one line where the two inputs were identical (no diffs), followed by two lines whose index values exist only in "file 1". (And what do you mean, exactly, by "unless one of the ids was blank"?)

Maybe part of the problem is that you don't have an accurate and coherent spec for what the script is supposed to do? If there really are just two inputs, and those three lines you show under "Final:" are really the correct desired output, then it looks like the spec would be something like this:

For each line in File 1, print it to Final if: (a) the ID/Key value and data are identical to a line in File 2, or (b) the ID/Key value is not found in File 2.

For that, the following is one way to do it:

use strict; my ( $file1, $file2 ) = @ARGV; # (getting file names from command line is better than hard-coding the +m) # read file2 first, to get the keys and data to test against my %refdata; open( F, $file2 ) or die "$file2: $!"; while (<F>) { my ( $key, $data ) = split( /,/, $_, 2 ); # (in case key is not 4 + digits) $refdata{$key} = $data; } # now read file1, and output lines that meet the spec open( F, $file1 ) or die "$file1: $!"; while (<F>) { my ( $key, $data ) = split( /,/, $_, 2 ); print if ( !exists( $refdata{$key} ) or $data eq $refdata{$key} ); } # (use the command line to redirect output to a "final" file -- e.g.: # # shell> perl your_script file1 file2 > final # # again, it's better than hard-coding another file name

In reply to Re: Comparing lines of multiple files by graff
in thread Comparing lines of multiple files by oomwrtu

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.