"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


In reply to Re^2: Hash Multiple values for a key-Filtering unique values for a key in hash by kcott
in thread Hash Multiple values for a key-Filtering unique values for a key in hash by rahulme81

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.