in reply to Looping two files differently

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.

Replies are listed 'Best First'.
Re^2: Looping two files differently
by noob_mas (Novice) on Jan 03, 2014 at 02:11 UTC

    Hi VincentK, "you are comparing each line in your first file to every line in your second file"

    Yes , i have a "config" file where i can configure the content of the file according to some element/name or etc. then this config file will search through another file(second file) line by line to check if the element/name from my config file exist or not. If yes, then the line will processed further, else if not then it will flag an error message.

    "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?"

    YES exactly, let say in the case of duplicate then it doesnt matter because it will be overwritten because i assigned it to a hash

    Thank you for your help and the sub that you give as well. I can use the sub as well with a bit of tweak. Thank you