in reply to Compare two files with Japanese and English data

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