Here's the situation:
I've got two versions of a novel, both in txt format. One is in the original language and the other in Chinese or English translation.When reading the original version, it sometimes happens I want to take a quick look at the translated version of a particular sentence. What I expect is: a corresponding sentence from the translated version directly pops into my eyes when I type that particular sentence in its original language.
Here's my approach:
My orginal thinking was that since Perl knows the position of the line that matches the query (#learnt this from Chris' solution to my second post), all I need to do is let Perl use that position information to display the content of another file. But then I realized shifting from one language to another is way more complicated. One single line of content in one language may turn out to be two or even three lines in another language and the difference will build up. Then I figured brian's solution to my third question seems to be useful again. One paragraph of content in one language is likely to be contained in equally one paragraph when translated. I can just let Perl treat a paragraph as a line. Now I've come with the following code.
Here's my code:
#! perl
use warnings; use strict;
use autodie;
my $n;
my $file1 = "c:/FR.txt";
my $file2 = "c:/EN.txt";
print "INPUT YOUR QUERY:";
chomp(my $query=<STDIN>);
open my $fr,'<', $file1;
{ local $/="\n\n"; #learnt from brians's solution to [my 3rd question][1]
my @fr = <$fr>;
close $fr;
for (0 .. $#fr) { #learnt from Chris' solution to [my 2nd question][2]
if ($fr[$_] =~ /$query/i){
$n = $_;
}
}
}
open my $eng,'<',$file2;
{ local $/="\n\n";
my @eng = <$eng>;
close $eng;
print $eng[$n];
}
Questions are here:
1: Is this a good approach to the problem?
2: When no match is found, I will receive a warning message saying something like "Use of uninialized value" etc.. Well,it's technical and yes I know the meaning. But is it possible to change this message to something like "Oops, no match is found"?
The test files are something like:
file1
Chapitre premier Une petite ville La petite ville de Verrières peut passer pour l’une des plus jolies de la Franche-Comté....Espagnols, et maintenant ruinées. Verrières est abrité du ... depuis la chute de Napoléon ...de presque toutes les maisons de Verrières. à peine entre-t-on dans la ville ... ... Eh ! elle est à M. le maire.
file2
CHAPTER 1 A Small Town The small town of Verrieres may be regarded as one of the most attractive....and now in ruins. Verrieres is sheltered ... since the fall of Napoleon, has led to the refacing of almost all the houses in Verrieres. No sooner has one entered the town ...Eh! It belongs to the Mayor.
If "La petite ville de" is searched, the output on screen should be:
The small town of Verrieres may be regarded as one of the most attractive....and now in ruins.
Thanks like always for any comments whatsoever :)
UPDATE1
Thanks for all the help!
Now question 2 can be solved with a few minor modifications like Chris has suggested:
if(defined $n) {
open my $eng,'<',$file2;
{ local $/="\n\n";
my @eng = <$eng>;
close $eng;
print $eng[$n];
}
} else {
print "Oops, no match found!\n";
}
UPDATE2
Chris' code should run much faster than mine when dealing a huge file.