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

One way:

c:\@Work\Perl\monks>perl -wMstrict -le "use List::MoreUtils qw(uniq); ;; use Data::Dump qw(dd); ;; my $hashref = { 'Alabama' => [ qw(Andalusia Anniston Clanton Eufaula Auburn Bessemer Eufaula Auburn Bessemer) ], 'California' => [ qw(Barstow Barstow) ], 'Georgia' => [ qw(Darien) ], 'New York' => [ 'Coney Island', qw(Amsterdam Beacon Becon), 'Coney Island', ], }; ;; make_uniq($hashref); dd $hashref; ;; ;; sub make_uniq { my ($hr) = @_; ;; $_ = [ uniq @$_ ] for values %$hr; } " { Alabama => [ "Andalusia", "Anniston", "Clanton", "Eufaula", "Auburn", "Bessemer", ], California => ["Barstow"], Georgia => ["Darien"], "New York" => ["Coney Island", "Amsterdam", "Beacon", "Becon"], }

Update: See List::MoreUtils::uniq()


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: Hash Multiple values for a key-Filtering unique values for a key in hash
by rahulme81 (Sexton) on May 23, 2017 at 03:49 UTC

    Thanks. How can i put this in my script ? is like below

    make_uniq(\%hashref); dd $hashref; sub make_uniq { my ($hr) = @_; $_ = [ uniq @$_ ] for values %$hr; }
      How can i put this in my script ?

      If your data is held in the form of a hash (not a hash reference), e.g.

      my %hash = ( 'Alabama' => [ qw(Andalusia Anniston Clanton Eufaula Auburn Bessemer Eufaula Auburn Bessemer) ], 'California' => [ qw(Barstow Barstow) ], 'Georgia' => [ qw(Darien) ], 'New York' => [ 'Coney Island', qw(Amsterdam Beacon Becon), 'Coney Island', ], );
      the following works (tested):
      make_uniq(\%hash); dd \%hash;


      Give a man a fish:  <%-{-{-{-<