in reply to Re: Unique keys for multiple values with a Hash
in thread Unique keys for multiple values with a Hash

The problem that I'm having is that all the values are being put on the array of values, and I just need the unique values I.E.
the Key is: 4 22 21 the Key is: 5 25 24 23 the Key is: 6 27 29 30

Replies are listed 'Best First'.
Re: Re: Re: Unique keys for multiple values with a Hash
by tall_man (Parson) on Jan 14, 2003 at 19:28 UTC
    Sorry, I missed that the first time (as did some others). Here is a quick fix. Replace the final loop with this:
    while ((my $key, my $value) = each %hash) { print "the Key is: $key\n"; my %seen = (); # Perl Cookbook 4.6 Extracting Unique Elements from a List my @uniq = grep { ! $seen{$_}++ } @$value; print join("\n",@uniq),"\n\n"; }
    Update: I see that Ovid also has a good one-pass solution.