There is a general pattern here, and it is this: this is a set intersection problem, and as such can be solved easily with Perl "sets", which we tend to call hashes :).
The approach is to create a set out of one of your data files (ie. indexing it into a hash table, like so: %set = (a => 1, b => 1, c => 1)
, where the actual value is immaterial, since we're going to be testing it with the "set membership operator" exists
).
Once you have that, it is a simple matter of going through the other data set, and test membership of one of its keys (in your case, source IDs) in the %set
above.
So, assuming the channel map file is in the following format:
ID,NAME
and the source ID file is just one ID per line, the you could do the following:
open my $CHANNEL_MAP, q{<}, $channel_map_file_name or die $!;
my %channel_map
= map {
chomp;
my @mapping = split /,/;
$mapping[0] => $mapping[1] # not needed, just being explicit.
} <$CHANNEL_MAP>;
close $CHANNEL_MAP or die $!;
# source ID file passed as a command line argument
while (<>) {
chomp;
print "$_ = $channel_map{$_}\n"
if exists $channel_map{$_};
}
However, this seems a bit pointless, since all you seem to want to do is to filter the channel map file by the existing source IDs. In that case, you could simply index the source ID file instead (use the technique above), and then simply go through the channel mappings file and print each line for which the ID exists in your source IDs.