in reply to Re: Confusion due to hash
in thread Confusion due to hash

reverse is useful to get the output in the desired format:
use strict; use warnings; use Data::Dumper; my %input = (10=>1, 20=>2, 30=>1, 40=>2); my %output; map { $output{ $input{ $_ } } += $_ } keys %input; %output = reverse %output; print Dumper \%output;
Prints:
$VAR1 = { '60' => '2', '40' => '1' };
Update: I paid to much attention to the requested output, and not enough to the note that he couldn't reverse the hash

Replies are listed 'Best First'.
Re^3: Confusion due to hash
by GrandFather (Saint) on Jan 31, 2006 at 01:51 UTC

    Except that OP says

    And how do I check if a value exists? I can not reverse the hash since it has many of the same values.

    in which case leaving %rhash in its original form allows the lookup that OP wants - no reversing required.


    DWIM is Perl's answer to Gödel