Use a constructed regular expression to perform the matching on the keys:

use strict; use warnings; use encoding 'UTF8'; my $aData = <<ADATA; This is a boy. &#12495;&#12531;&#12459; She is a girl. I am a man. This apple is big. That orange is small. ADATA my $bData = <<BDATA; is a &#12495;&#12531;&#12459; small BDATA open my $inB, "<:utf8", \$bData; my (@keys) = <$inB>; close ($inB); chomp @keys; my $match = join '|', sort {length $b <=> length $a} @keys; $match = qr{($match)}; open my $inA, "<:utf8", \$aData; while (defined (my $line = <$inA>)) { print "Found '$1' in line $.:\n $line" if $line =~ $match; }

Prints:

Found 'is a' in line 1: This is a boy. Found '&#12495;&#12531;&#12459;' in line 2: &#12495;&#12531;&#12459; Found 'is a' in line 3: She is a girl. Found 'is a' in line 5: This apple is big. Found 'small' in line 6: That orange is small.

Note: I don't seem to be able to paste ハンカ into the code blocks. Replace the entities with the actual string for correct behaviour.

True laziness is hard work

In reply to Re: Compare two files with Japanese and English data by GrandFather
in thread 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.