Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Remove duplicates from Hash of Arrays

by Anonymous Monk
on Dec 09, 2013 at 20:28 UTC ( [id://1066330]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello, this is probably a simple question but I'm a bit lost. I have a Hash of Arrays structure and I want to get every element of the arrays in one single array. This works but it doesn't remove the duplicates. Is there a simple way to remove duplicates at the same time?
my $HoA = { 'music' => [9, 53, 3, 15], 'conferences' => [7, 56, 15], }; my @ids; for my $ary ( values %$HoA ) { push(@ids,@$ary); } print "@ids"; # 9 53 3 15 7 56 15

Replies are listed 'Best First'.
Re: Remove duplicates from Hash of Arrays
by Kenosis (Priest) on Dec 09, 2013 at 20:42 UTC

    Perhaps the following will be helpful:

    use strict; use warnings; my %h; my $HoA = { 'music' => [ 9, 53, 3, 15 ], 'conferences' => [ 7, 56, 15, 53 ], }; @h{@$_} = undef for values %$HoA; my @ids = keys %h; print "@ids"; # 53 56 3 7 9 15

    What is this @h{@$_} = undef notation? See Hash from two arrays.

      Thanks, I will use this. After some reading up it seems setting each key to undefined can save memory when dealing with large arrays, as I sometimes will be.
Re: Remove duplicates from Hash of Arrays
by hdb (Monsignor) on Dec 09, 2013 at 20:45 UTC
    my @ids = keys %{{ map { $_ => 1 } map { @$_ } values %$HoA }};
Re: Remove duplicates from Hash of Arrays
by LanX (Saint) on Dec 09, 2013 at 20:41 UTC
    yes, instead of pushing to an array set keys in a hash.

    keys %hash will return you a set w/o duplicates.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re: Remove duplicates from Hash of Arrays
by wind (Priest) on Dec 09, 2013 at 22:16 UTC
Re: Remove duplicates from Hash of Arrays
by Anonymous Monk on Dec 09, 2013 at 21:53 UTC
    Thank you for your helpful and informative replies

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2026-05-12 22:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.