Connecting Tech Pros Worldwide Forums | Help | Site Map

Problems in comparing two files written in Japanese

Newbie
 
Join Date: Jul 2009
Posts: 1
#1: Jul 16 '09
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.

The code I am using is the one pasted below:



Expand|Select|Wrap|Line Numbers
  1. #!C:\perl\bin\perl.exe
  2. # file_compare.pl
  3. # Purpose: compare two files(file_1,file_2) and show differences
  4.  
  5.  
  6. use strict;
  7. use warnings;
  8.  
  9. my $file1 ='E:\perl_folder\file_1.txt' or die "filename missing \n";
  10. my $file2 ='E:\perl_folder\file_2.txt'  or die "filename missing \n";
  11.  
  12. open (FILE1, "< $file1") or die "Can not read file $file1: $! \n";
  13. my @file1_contents = <FILE1>; # read entire contents of file
  14. close (FILE1);
  15.  
  16. open (FILE2, "< $file2") or die "Can not read file $file2: $! \n";
  17. my @file2_contents = <FILE2>; # read entire contents of file
  18. close (FILE2);
  19.  
  20. my $length1 = $#file1_contents; # number of lines in first file
  21. my $length2 = $#file2_contents; # number of lines in second file
  22.  
  23. if ($length1 > $length2) {
  24. # first file contains more lines than second file
  25. my $counter2 = 0;
  26. foreach my $line_file1 (@file1_contents) {
  27. chomp ($line_file1);
  28.  
  29. if (defined ($file2_contents[$counter2])) {
  30. # line exists in second file
  31. chomp (my $line_file2 = $file2_contents[$counter2]);
  32.  
  33. if ($line_file1 ne $line_file2) {
  34. print "\nline " . ($counter2 + 1) . " \n";
  35. print "< $line_file1 \n" if ($line_file1 ne ""); 
  36. print "--- \n";
  37. print "> $line_file2 \n\n" if ($line_file2 ne "");
  38. }
  39. }
  40. else {
  41. # there is no line in second file
  42. print "\nline " . ($counter2 + 1) . " \n";
  43. print "< $line_file1 \n" if ($line_file1 ne ""); 
  44. print "--- \n";
  45. print "> \n"; # this line does not exist in file2
  46. }
  47. $counter2++; # point to the next line in file2
  48. }
  49. }
  50.  

KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#2: Jul 16 '09

re: Problems in comparing two files written in Japanese


if you get no replies here try on www.perlmonks.com or www.stackoverflow.com

I personally don't know how to do what you are asking.
Reply

Tags
file comparision