Hi, I got the code (from the internet)for comparing two files and showing the difference in contents.Now,I tried the same code for two files written in japanese language(kanji).If I save the two japanese .txt files in ANSI format,it works fine,but, if I save them in formats like 'UTF-8','unicode','unicode bigendian',it doesn't show the differences properly....keeps showing odd symbols instead of the japanese characters. Would be glad if someone could suggest some simple way of making it work for all formats(if it's possible). The code I am using is the one pasted below:
#!C:\perl\bin\perl.exe # file_compare.pl # Purpose: compare two files and show differences use strict; use warnings; my $file1 ='E:\files\file_1.txt' or die "filename missing \n"; my $file2 = 'E:\files\file_2.txt' or die "filename missing \n"; open (FILE1, "< $file1") or die "Can not read file $file1: $! \n"; my @file1_contents = <FILE1>; # read entire contents of file close (FILE1); open (FILE2, "< $file2") or die "Can not read file $file2: $! \n"; my @file2_contents = <FILE2>; # read entire contents of file close (FILE2); my $length1 = $#file1_contents; # number of lines in first file my $length2 = $#file2_contents; # number of lines in second file if ($length1 > $length2) { # first file contains more lines than second file my $counter2 = 0; foreach my $line_file1 (@file1_contents) { chomp ($line_file1); if (defined ($file2_contents[$counter2])) { # line exists in second file chomp (my $line_file2 = $file2_contents[$counter2]); if ($line_file1 ne $line_file2) { print "\nline " . ($counter2 + 1) . " \n"; print "< $line_file1 \n" if ($line_file1 ne ""); print "--- \n"; print "> $line_file2 \n\n" if ($line_file2 ne ""); } } else { # there is no line in second file print "\nline " . ($counter2 + 1) . " \n"; print "< $line_file1 \n" if ($line_file1 ne ""); print "--- \n"; print "> \n"; # this line does not exist in file2 } $counter2++; # point to the next line in file2 } } else { # second file contains more lines than first file # or both have equal number of lines my $counter1 = 0; foreach my $line_file2 (@file2_contents) { chomp ($line_file2); if (defined ($file1_contents[$counter1])) { # line exists in first file chomp (my $line_file1 = $file1_contents[$counter1]); if ($line_file1 ne $line_file2) { print "\nline " . ($counter1 + 1) . " \n"; print "< $line_file1 \n" if ($line_file1 ne ""); print "--- \n"; print "> $line_file2 \n" if ($line_file2 ne ""); } } else { # there is no line in first file print "\nline " . ($counter1 + 1) . " \n"; print "< \n"; # this line does not exist in file1 print "--- \n"; print "> $line_file2 \n" if ($line_file2 ne ""); } $counter1++; # point to next line in file1 } }
Thanks in advance for any help.

In reply to Problems in comparing two files written in Japanese by Rishiraj

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.