in reply to Comparing values in a hash

Do it by inverting the hash:
my %revhash; while (my($k, $v) = each %hash) { push @{$revhash{$v}}, $k; } for my $val (sort { $a <=> $b } keys %revhash) { my @keys = @{$revhash{$_}}; if (@keys > 1) { # only items where there's a tie print "$val is shared by @keys\n"; } }

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re: &bull;Re: Comparing values in a hash
by VSarkiss (Monsignor) on Sep 06, 2002 at 00:21 UTC

    I presume you're doing the first each loop for clarity of exposition; can't you just do: my %revhash = reverse %hash;Or am I missing something?

Re: &bull;Re: Comparing values in a hash
by cens (Novice) on Sep 06, 2002 at 00:07 UTC
    Thank you very much. I feel like an idiot...should have known that.