note
Loops
<p>Hello. You lost me a bit about the mapping between the input text file and the hash. The values don't match and yet you want Key1 and Key3 in your output? I'm going to assume that was a typo, and what you're asking is to match that first string on each input line to the <b>values</b> contained in your hash:</p>
<c>
my %hash = ( Key1 => 'Pigeon.Lion.Tiger.Elephant',
Key2 => 'Pigeon.Lion.Tiger.Rabbit',
Key3 => 'Pigeon.Lion.Tiger.Monkey',
);
my %lookup = reverse %hash;
while (<DATA>) {
chomp;
my ($value) = split;
my $key = $lookup{$value};
print "$_ ($key)\n" if /SOME_DATA/ and defined $key;
}
__DATA__
Pigeon.Lion.Tiger.Elephant SOME_DATA
Pigeon.Lion.Tiger.Lion SOME_DATA
Pigeon.Lion.Tiger.Monkey SOME_DATA
</c>
<p>Prints:</p>
<c>
Pigeon.Lion.Tiger.Elephant SOME_DATA (Key1)
Pigeon.Lion.Tiger.Monkey SOME_DATA (Key3)
</c>
<p>Hopefully that looks right to you. The main hash is reversed so we can use %lookup to find matching values and discover the associated key. Reverse will put the values into the keys, with their respective keys as values. Everything else falls into place.</p>
1108186
1108186