in reply to Re: Re: Reversing Hash
in thread Reversing Hash
Hi.
This solution assumes that you don't mind duplicate values (i.e. where two unique keys from %hash have the same value) being pushed onto an array.
Also, feel free to change the value of $; to whatever you like if a space isn't suitable.
use strict; my %rev_hash = (); my %hash = ( 'key1' => [1, 20, 3], 'key2' => [1, 30, 4, 7], 'key3' => [1, 9, 8], 'key4' => [1, 9, 17], 'key5' => [1, 9, 17], ); { local ($;) = " "; while (my ($k, $v) = each %hash) { push @{$rev_hash{"@{$v}"}}, $k; } while (my ($k, $v) = each %rev_hash) { print qq(\$rev_hash{$k} = "@{$v}"\n); } }
Cheers,
-- Dave :-)
$q=[split+qr,,,q,~swmi,.$,],+s.$.Em~w^,,.,s,.,$&&$$q[pos],eg,print
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Re: Reversing Hash
by artist (Parson) on Mar 03, 2003 at 10:41 UTC |