Hi waytoperl,
Considering, the __DATA__ and the hash data presented, there is no way you are getting this desired output
Pigeon.Lion.Tiger.Elephant SOME_DATA (Key1)
Pigeon.Lion.Tiger.Monkey SOME_DATA (Key3)
Why?
- If "Pigeon." and " SOME_DATA" is taken away from your input coming from __DATA__, compare with your hash data if "Cat.Mouse.Game." is taken away you practically have these:
- Lion.Tiger.Elephant
- Lion.Tiger.Lion
- Lion.Tiger.Monkey
-
With " SOME_DATA" common to all input, one cannot use it to differentiate, when comparing with the hash data given.
- And with the OP update given, all compared data will match, except if there is some kind of changed in input data. Then would the (key2) not match
Something like this:
use warnings;
use strict;
my %hash = (
'Key1' => 'Cat.Mouse.Game.Lion.Tiger.Elephant',
'Key2' => 'Cat.Mouse.Game.Lion.Tiger.Lion',
'Key3' => 'Cat.Mouse.Game.Lion.Tiger.Monkey',
);
while ( my $data = <DATA> ) {
chomp($data);
my $value = join '.' => ( split /[\s.]/, $data )[ 1 .. 3 ];
print map { "$data ($_)\n" if $hash{$_} =~ /$value/ } keys %hash;
}
__DATA__
Pigeon.Lion.Tiger.Elephant SOME_DATA
Pigeon.Lion.Tiger.SeaLion SOME_DATA
Pigeon.Lion.Tiger.Monkey SOME_DATA
OUTPUT
Pigeon.Lion.Tiger.Elephant SOME_DATA (Key1)
Pigeon.Lion.Tiger.Monkey SOME_DATA (Key3)
Please note that the input data has been changed from what the OP gave.
With the OP input, the output will be:
Pigeon.Lion.Tiger.Elephant SOME_DATA (Key1)
Pigeon.Lion.Tiger.Lion SOME_DATA (Key2)
Pigeon.Lion.Tiger.Monkey SOME_DATA (Key3)
If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author
unknown to me
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.