in reply to Confusion due to hash

I'm not quite sure where you are going with this, but it seems to me that you need two hashes:

use strict; use warnings; use Data::Dumper; my %hash= (10=>1, 20=>2, 30=>1, 40=>2); my %rhash; map {$rhash{$hash{$_}} += $_} keys %hash; print Dumper (\%hash); print Dumper (\%rhash);

Prints:

$VAR1 = '%hash'; $VAR2 = { '40' => 2, '30' => 1, '10' => 1, '20' => 2 }; $VAR1 = '%rhash'; $VAR2 = { '1' => 40, '2' => 60 };

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Confusion due to hash
by duckyd (Hermit) on Jan 31, 2006 at 01:44 UTC
    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

      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