in reply to Re: Hash Multiple values for a key-Filtering unique values for a key in hash
in thread Hash Multiple values for a key-Filtering unique values for a key in hash

"Can i delete this for one key only (no particular where i need to retain this value for which key, just need once in any key)"

I made reference to that in my original reply:

"To keep the values unique for a specific key, without removing duplicates from the values of other keys, you'll need to clear the %seen hash for each arrayref value."

In this code:

for (values %$HASH1) { my %seen; $_ = [ grep { ! $seen{$_}++ } @$_ ]; }

Just move the "my %seen;" line out of the loop:

my %seen; for (values %$HASH1) { $_ = [ grep { ! $seen{$_}++ } @$_ ]; }

Which, as the loop now only contains one statement, you can write more succinctly as:

my %seen; $_ = [ grep { ! $seen{$_}++ } @$_ ] for values %$HASH1;

I put that into the original code I gave you (along with your new HoA). Here's the output:

{ "Alabama" => ["Andalusia", "Anniston", "Clanton", "Eufaula", "Aub +urn"], "California" => ["Barstow"], "New York" => ["Amsterdam", "Coney Island", "Beacon"], "Utah" => ["Layton"], }

Which exactly matches what you have for "so my result hash should look like".

By the way, purely for academic interest, because I can't see that it gains you anything in this particular instance, if you're using Perl 5.10, or later, you could have reduced those two lines to this one:

$_ = [ grep { state %seen; ! $seen{$_}++ } @$_ ] for values %$HASH1;

I tested that: the output is identical. See state for more details.

— Ken