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