Do you know what character encoding was used to write the "Japanese compressed file"? Is it Unicode (utf8, utf16)? (Shift) JIS? EUC? Something else?
If you don't know how to answer that question, your first task is to figure out how to answer that question. If you don't have some reliable reference or documentation for your data that answers the question, use Encode::Guess for that. Try it, and if you can't figure it out, show us what you tried.
As for the rest, it sounds like you want to read file B first, store its "rid" values as hash keys (and if there's anything else you need from B in your output, store that as hash values).
Then read each A file in turn, and output whatever you want when there's a suitable match to one of the hash keys from file B. Your description is not clear, but I gather that the logic would be something like:
use strict;
my %B_rids;
# ... open file B, read it to fill %B_rids hash, then:
my @A_files = ... # do something to get list of A file paths/names
for my $A_file ( @A_files ) {
unless ( open( my $afh, "<", $A_file )) {
# update: you'll add encoding on 2nd arg, e.g. "<:utf8"
warn "Open failed for $A_file: $!\n";
next;
}
while (<$afh>) {
chomp;
my @fields = split /\t/;
if ( exists( $B_rids{ $fields[4] } )) { # does rid1 exist in B
+?
# do something
}
elsif ( exists( $B_rids{ $fields[5] } )) { # does rid2 exist i
+n B?
# do something else
}
}
}
(updated to fix typo in list of encodings)