Dear all,

I am a newbie of Perl. I am asked to compare two files which contain some Japanese and English informaion. I use a slow approach, read two files and put them in two arrays, then use while loop to check whether the File_B's TERMS are WITHIN the lines of File_A.

File_A:

This is a boy.

ハンカ

She is a girl.

I am a man.

This apple is big.

That orange is small.

File_B:

is a

ハンカ

small

Output:

I am a man.

This apple is big.

The program I wrote works fine. However, the files I am processing are very huge. The running time is so slow if I use looping like this. My friend told me that I can put the files into hash and it will run far much quicker. I can see the point of using it but the files I have are random and also the terms in File_B can be in any part of File_A. Some previous posts suggest that I can use split the lines of File_A, put the line in hash and compare it with File_B (put File_B in the other hash as well). However, I do not know how to do it besides using ~// to check whether the line got the term.

Thanks very much for your help indeed!

Kind regards,

Luke

open(A_FILE, "<", "FILE_A.txt"); my(@a_lines) = <A_FILE>; # read file into list close(A_FILE); open(B_FILE, "<", "FILE_B.txt"); my(@b_lines) = <B_FILE>; # read file into list my($b_lines); close(B_FILE); open(my $out, ">", "Useful.txt") or die "Can't open Useful.txt: $!"; $number = @b_lines; foreach $a_line (@a_lines) # loop thru list { $found = 0; my $sentence = $a_line; $i = 0; chomp($sentence); while (($i <= $number-1) and ($found == 0)){ chomp($b_lines[$i]); if ($sentence =~ /$b_lines[$i]/){ $found = 1; } $i++; } if ($found == 1) { print $out $sentence."\n"; } }

In reply to Compare two files with Japanese and English data by solukas

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.