davidos code is way more elegant, if you employ matijas fix, but since I wrote it, here is a rather ugly solution:
my %hash2 = (); my %unique = (); foreach my $test (grep(!$unique{$_}++, values %hash)) { $hash2{$test} .= join(",", grep({$hash{$_} eq $test ? hash{$_} : u +ndef} keys %hash)); }
And since I was wondering, it isn't slower than davidos solution, the solutions seem to be equally efficient:
use strict; use warnings; use Data::XDumper qw(Dump); use Benchmark qw(:all); my %hash = ( ball1 => 'red', ball2 => 'blue', ball3 => 'red', ); print scalar Dump(\%hash); my %hash2 = (); my %unique = (); my %reverse_hash; cmpthese(1000000, { 'unique_grep' => sub { %unique = (); %hash2 = (); foreach my $test (grep(!$unique{$_}++, values %hash)) +{ $hash2{$test} .= join(",", grep({$hash{$_} eq $tes +t ? $hash{$_} : undef} keys %hash)); } }, 'reverse_hash' => sub { %reverse_hash = (); while ( my ( $key, $value ) = each %hash ) { push @{$reverse_hash{$value}}, $key; } }, }); __END__ 1st run: Rate unique_grep reverse_hash unique_grep 29940/s -- -2% reverse_hash 30694/s 3% -- 2nd run: Rate reverse_hash unique_grep reverse_hash 28466/s -- -2% unique_grep 29104/s 2% --
regards,
tomte
Hlade's Law:
If you have a difficult task, give it to a lazy person --
they will find an easier way to do it.
In reply to Re: mildly tricky hash question
by Tomte
in thread mildly tricky hash question
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |