Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Array and Hash

by 2teez (Vicar)
on Nov 24, 2014 at 02:51 UTC ( #1108218=note: print w/replies, xml ) Need Help??

in reply to Array and Hash

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)
  • 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:
    1. Lion.Tiger.Elephant
    2. Lion.Tiger.Lion
    3. 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
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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1108218]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2021-04-10 21:50 GMT
Find Nodes?
    Voting Booth?

    No recent polls found