in reply to Array and Hash
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 values contained in your hash:
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
Prints:
Pigeon.Lion.Tiger.Elephant SOME_DATA (Key1) Pigeon.Lion.Tiger.Monkey SOME_DATA (Key3)
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.
|
---|
In Section
Seekers of Perl Wisdom