in reply to read hash table from a subroutine or retrieve previously stored hash table from a file
If I'm understanding you correctly, perhaps the following will be helpful:
use warnings; use strict; use Data::Dumper; my ( %hashTable, %groupedHash ); while (<DATA>) { my ( $key, $val ) = split; $hashTable{$key} = $val; } groupByCol2( \%hashTable, \%groupedHash ); print Dumper \%groupedHash; sub groupByCol2 { my ( $hashTableRef, $groupedHashRef ) = @_; for my $key ( keys %$hashTableRef ) { push @{ $$groupedHashRef{ $$hashTableRef{$key} } }, $key; } } __DATA__ name1 group1 name2 group2 name3 group1 name4 group4 name5 group2 name6 group7 name7 group4 name8 group5
Output:
$VAR1 = { 'group4' => [ 'name7', 'name4' ], 'group2' => [ 'name2', 'name5' ], 'group7' => [ 'name6' ], 'group1' => [ 'name1', 'name3' ], 'group5' => [ 'name8' ] };
Send two hash references to the subroutine groupByCol2--the first a reference to your 'hash table' with repeating values and the second to a hash for a hash of arrays (HoA). The second has holds the 'grouped' names.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: read hash table from a subroutine or retrieve previously stored hash table from a file
by waytoperl (Beadle) on Nov 19, 2013 at 21:27 UTC | |
by Kenosis (Priest) on Nov 19, 2013 at 22:26 UTC |