Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Unique key/value pair in Multiple key values pair in Hash

by roboticus (Canon)
on Feb 18, 2013 at 13:49 UTC ( #1019343=note: print w/ replies, xml ) Need Help??


in reply to Unique key/value pair in Multiple key values pair in Hash

Seek_Knowledge:

If you're interested in the unique values, I'd suggest adding another level to your hash, something like:

use strict; use warnings; my %hash; while (<DATA>) { s/\s+$//; my ($key1, $key2) = split /\s+/, $_; $hash{$key1}{$key2}++; } for my $k1 (sort keys %hash) { print "$k1: ", join(", ", sort keys %{$hash{$k1}}), "\n"; } __DATA__ Lemon Juice Lemon Cake Apple Cider Lemon Cake Apple Butter Apple Sauce Apple Juice Lemon Juice

Note: The code is untested, but I'd expect output like:

Apple: Butter, Cider, Juice, Sauce Lemon: Cake, Juice

...roboticus

When your only tool is a hammer, all problems look like your thumb.


Comment on Re: Unique key/value pair in Multiple key values pair in Hash
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2014-10-25 12:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (143 votes), past polls