in reply to Re^3: comparing any two text files and writing the difference to a third file
in thread comparing any two text files and writing the difference to a third file

Here is a simplistic algorithm that will just compare each line, one at a time, and show whether the individual lines match or not.

use warnings; use strict; use autodie; use File::Compare; my $F1="version1.txt"; my $F2="count.txt"; my $F3="differ.txt"; ############################# #### adding these sections to create the files for me { open my $fh, '>', $F1; print {$fh} <<EOT; This is line one This is second line This is third line EOT } # automatically closes file when leaving scope { open my $fh, '>', $F2; print {$fh} <<EOT; This is line 1 This is secont line This is third line EOT } # automatically closes file when leaving scope ############################# my $cmp = compare($F1,$F2); # [pryrt]: if they're big, it's better to +compare once, rather than three times #the addition of the USE line is important for this function to wo +rk. if($cmp==0) {print"they are the same\n";} elsif($cmp==1) {print"they are different\n"; #opening/creating all three open my $fh1, '<', $F1; open my $fh2, '<', $F2; open my $fh3, '>', $F3; while (<$fh1>) { last if eof($fh2); my $content2=<$fh2>; #reads the complete file to content2. + # pryrt: add 'my' for lexical scope chomp($_, $content2); #haven't yet figured out how to pull out the difference data. #### [pryrt]: here's a simple comparison, just highlighting w +hich lines are differnt if($_ eq $content2) { # this line is the same printf $fh3 "%-20s= %s\n", 'MATCH', $_; } else { # this line is different printf $fh3 "%-20s> %s\n", 'DIFFERENT', ''; printf $fh3 "%20s< %s\n", $F1, $_; printf $fh3 "%20s> %s\n", $F2, $content2; } #### [/pryrt] } } elsif($cmp==-1) {print"error\n";} else {print"something wrong\n";} print"Please check the file $F3\n"; # pryrt: don't repeat yours +elf: use the filename variable ### pryrt: I want to display F3 without external help { print "\n===== $F3 =====\n"; open my $fh, '<', $F3; print while(<$fh>); # uses perlish postfix, and perlish default of + print using $_ print "\n===============\n"; }

with output:

they are different Please check the file differ.txt ===== differ.txt ===== DIFFERENT > version1.txt< This is line one count.txt> This is line 1 DIFFERENT > version1.txt< This is second line count.txt> This is secont line MATCH = This is third line ===============

Oh, right, aside from the comments I made, I also added newlines to your print statements.