Hi noob_mas,

It looks like you are comparing each line in your first file to every line in your second file. In this case, a match in the first file say on line two could match line 50 in file two ( or multiple lines if files contain duplicates ). Is this what you want?

If you want a line by line comparison, I would compare each line in order between the files and then note the differences.

Here is a sub that given the file handles for the input and output files will write out the results of the file comparison to the output file. I am sure there is a more efficient way to do this , perhaps using Array Compare http://search.cpan.org/~davecross/Array-Compare-2.02/lib/Array/Compare.pm, but this does seem to work. The sub will use the largest file for the compare. It will stop processing if it reaches the end of the smaller file before the larger file.

sub compare_files($$$) { my $FH1 = shift; my $FH2 = shift; my $OUTPUT_FH = shift; my @FILE1_ARRY = <$FH1>; my @FILE2_ARRY = <$FH2>; my $CMPR_ARRY_REF = \@FILE1_ARRY; my @CMPR_ARRY = @{$CMPR_ARRY_REF}; my $CMPR_ARRY_NUMBER = 1; if ($#FILE1_ARRY > $#FILE2_ARRY) { print "FILE 1 is larger than FILE 2\n"; print $OUTPUT_FH "FILE 1 is larger than FILE 2\n"; } elsif ($#FILE1_ARRY < $#FILE2_ARRY) { print "FILE 1 is smaller than FILE 2\n"; print $OUTPUT_FH "FILE 1 is smaller than FILE 2\n"; $CMPR_ARRY_REF = \@FILE2_ARRY; @CMPR_ARRY = @{$CMPR_ARRY_REF}; $CMPR_ARRY_NUMBER = 2; } else { print "FILE 1 is the same size as FILE 2\n"; print $OUTPUT_FH "FILE 1 is the same size as FILE 2\n"; } print "*"x70,"\n"; print $OUTPUT_FH "*"x70,"\n"; for(my $iter = 0; $iter <= $#CMPR_ARRY; $iter++) { if ( $CMPR_ARRY_NUMBER == 1 ) { print "END OF FILE 2, BUT NOT FILE 1. STOPPING COMPARE AT +LINE ".($iter+1)." OF FILE 1\n" if ($iter > $#FILE2_ARRY); print $OUTPUT_FH "END OF FILE 2, BUT NOT FILE 1. STOPPING +COMPARE AT LINE ".($iter+1)." OF FILE 1\n" if ($iter > $#FILE2_ARRY); last if ($iter > $#FILE2_ARRY); if ( quotemeta($CMPR_ARRY[$iter]) eq quotemeta($FILE2_ARRY +[$iter]) ) { print $OUTPUT_FH "Line ".($iter+1)." matches between b +oth files\n"; } else { print $OUTPUT_FH "Line ".($iter+1)." does NOT match be +tween both files\n"; } } else { print "END OF FILE 1, BUT NOT FILE 2. STOPPING COMPARE AT +LINE ".($iter+1)." OF FILE 2\n" if ($iter > $#FILE1_ARRY); + print $OUTPUT_FH "END OF FILE 1, BUT NOT FILE 2. STOPPING +COMPARE AT LINE ".($iter+1)." OF FILE 2\n" if ($iter > $#FILE1_ARRY); last if ($iter > $#FILE1_ARRY); if ( quotemeta($CMPR_ARRY[$iter]) eq quotemeta($FILE1_ARRY +[$iter]) ) { print $OUTPUT_FH "Line ".($iter+1)." matches between b +oth files\n"; } else { print $OUTPUT_FH "Line ".($iter+1)." does NOT match be +tween both files\n"; } } } print "*"x70,"\nCompare Complete."; print $OUTPUT_FH "*"x70,"\nCompare Complete."; }

Once in place, you can call with sub with :

compare_files($FH1,$FH2,$OUTPUT_FH);

I hope this helps. If not, maybe you can modify this sub to further suit your needs.

In reply to Re: Looping two files differently by VincentK
in thread Looping two files differently by noob_mas

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.