By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,852 Members | 2,259 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,852 IT Pros & Developers. It's quick & easy.

Problems in comparing two files written in Japanese

P: 1
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.  
Jul 16 '09 #1
Share this Question
Share on Google+
1 Reply


KevinADC
Expert 2.5K+
P: 4,059
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.
Jul 16 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.