myfrndjk has asked for the wisdom of the Perl Monks concerning the following question:

Hi i want to compare two html files character by character and print the difference if there is any difference between two files.Thanks

use strict; use warnings; use List::Compare; use LWP::Simple; open F,"<","C:/Users/jeyakuma/Desktop/shipping project/changed co +ntents/file1.html" ; open S, "<","C:/Users/jeyakuma/Desktop/shipping project/database/fi +le2.html" ; my @a=<F>; my @b=<S>; my $lc = List::Compare->new(\@a,\@b); my @intersection = $lc->get_intersection; my @firstonly = $lc->get_unique; my @secondonly = $lc->get_complement; print "Common Items:\n"."@intersection"."\n"; print "only in file\n"."\n@firstonly"."\n"; print "only in file1 \n"."\n@secondonly"."\n";

file1

There are 2 mangoes in the basket and There are two apples in the basket

file2

They are 3 mangoes in the basket and They are six grapes in the basket

Expected output

only in file1-->2, two

only in file2-->3 six grapes

common content in both -->There are mangoes in the basket and There are apples in the basket

current result

only in file1

There are 2 mangoes in the basket and There are two apples in the basket

only in file2

They are 3 mangoes in the basket and They are six grapes in the basket

common

(blank)

Replies are listed 'Best First'.
Re: compare and find difference between two files
by GotToBTru (Prior) on Jul 14, 2014 at 18:18 UTC

    Verify using debugger or Data::Dumper that @a and @b contain what you think.

    1 Peter 4:10
      Thanks for your reply .It works.
      use Data::Dumper qw(Dumper); use strict; use warnings; use List::Compare; open F,"<","C:/Users/jeyakuma/Desktop/file1.txt" ; open S, "<","C:/Users/jeyakuma/Desktop/file.txt" ; my $str = <F>; my $str1=<S>; my @old = split / /, $str; my @new=split / /, $str1; my $lc = List::Compare->new(\@old,\@new); my @intersection = $lc->get_intersection; my @firstonly = $lc->get_unique; my @secondonly = $lc->get_complement; print "Present only in old dump data@firstonly\n"; print "Present only in new data @secondonly\n";
Re: compare and find difference between two files
by zentara (Cardinal) on Jul 14, 2014 at 17:59 UTC
Re: compare and find difference between two files
by Anonymous Monk on Jul 14, 2014 at 20:09 UTC
    Uhhhh.... what about the 30-year-old Unix command... diff ? ? ?

      Looks like the OP is on Windows, so diff probably isn't available. However, the CPAN module Text::Diff implements a somewhat scaled-back diff in Perl so it's cross-platform.

        There's msys for diff and other common Unix tools for Windows, or what about fc.exe? That command has been around since DOS. Did Windows 7 and 8 do away with it?