Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Just another Perl shrine
 
PerlMonks  

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

by Seek_Knowledge (Initiate)
on Feb 18, 2013 at 10:17 UTC ( #1019315=perlquestion: print w/ replies, xml ) Need Help??
Seek_Knowledge has asked for the wisdom of the Perl Monks concerning the following question:

Hi Experts, I am beginner in Perl, kindly help me with this query. I have a multiple key value hash like below.
push(@{$hash{'Lemon'}}, 'Juice'); push(@{$hash{'Lemon'}}, 'Cake'); push(@{$hash{'Lemon'}}, 'Juice'); push(@{$hash{'Lemon'}}, 'Cake');
I want to find unique set in the above hash like below
Lemon -> Juice Lemon -> Cake
Please help me with this

Comment on Unique key/value pair in Multiple key values pair in Hash
Select or Download Code
Re: Unique key/value pair in Multiple key values pair in Hash
by choroba (Abbot) on Feb 18, 2013 at 10:24 UTC
    When you need something unique, think a hash:
    #!/usr/bin/perl use warnings; use strict; use feature qw(say); my %hash; push(@{$hash{Lemon}}, 'Juice'); push(@{$hash{Lemon}}, 'Cake'); push(@{$hash{Lemon}}, 'Juice'); push(@{$hash{Lemon}}, 'Cake'); my %uniq; undef @uniq{@{$hash{Lemon}}}; say for keys %uniq;

    Do you really need the array? If not, you can use HoH instead of HoA:

    undef $hash{Orange}{Cake}; undef $hash{Orange}{Juice}; undef $hash{Orange}{Cake}; undef $hash{Orange}{Juice}; say for keys %{ $hash{Orange} };
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Unique key/value pair in Multiple key values pair in Hash
by DrHyde (Prior) on Feb 18, 2013 at 11:47 UTC

    What have you tried so far, and how do the results differ from what you expected?

    Absent this information, we can't help you. We could tell you what to do, but that wouldn't help you to learn.

Re: Unique key/value pair in Multiple key values pair in Hash
by BillKSmith (Hermit) on Feb 18, 2013 at 13:43 UTC
Re: Unique key/value pair in Multiple key values pair in Hash
by roboticus (Canon) on Feb 18, 2013 at 13:49 UTC

    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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (12)
As of 2014-04-17 08:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (441 votes), past polls