Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Array and Hash

by Loops (Curate)
on Nov 23, 2014 at 22:49 UTC ( [id://1108189]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-19 19:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found