in reply to Re^2: Need to find unique values in hash
in thread Need to find unique values in hash

Not sure which way you want the grouping count1 or count2.

#!perl use strict; use Data::Dumper; my %hash = ( '33|srv2' => [ 'users','users','users', 'admin','admin','admin', 'manager','manager','manager' ], '27|rufserv3' => ['system'], '16|lbapp0112' => [ 'admin (priv1', ' priv2)' ], '34|srv2' => [ 'users', 'users', 'users', 'admin', 'admin', 'manager' ] ); #print Dumper %hash; my %count1; my %count2; for my $key (keys %hash){ ++$count1{$_}{$key} for @{$hash{$key}}; ++$count2{$key}{$_} for @{$hash{$key}}; } print Dumper \%count1; print Dumper \%count2;
poj

Replies are listed 'Best First'.
Re^4: Need to find unique values in hash
by dipit (Sexton) on Feb 05, 2019 at 09:54 UTC

    Just WOW! From the beginning i am stucked at this stage only where i need to print the duplicated values. Not finding a way how can i iterate it! Really thanks for the solution. Cheers!

Re^4: Need to find unique values in hash
by dipit (Sexton) on Feb 05, 2019 at 08:00 UTC

    Thank you POJ. This really helped but i am still not able to figure out, how to print the duplicates? This way, only unique elements will be present in hash and i am successfully able to get the hash. Can you please help in providing how can i print which value/values are repeated for their respective keys?

    Well, I need to print above according to this :

     ++$count2{$key}{$_} for @{$hash{$key}};

      Replace print Dumper \%count2 with this code.

      for my $key1 (sort keys %count2){ for my $key2 (sort keys %{$count2{$key1}}){ if ($count2{$key1}{$key2} > 1){ printf "%-10s %-10s %d\n",$key1,$key2,$count2{$key1}{$key2}; } } }
      poj