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.

  • Comment on Re: read hash table from a subroutine or retrieve previously stored hash table from a file
  • Select or Download Code

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

    Thanks Kenosis. I've started working on my logic. I tried with you code, but it didnt parse keys and value correctly. Insted got output like:

    Output:

    is mixed with column1 and column2 data.

    $VAR1 = { 'group4' => [ 'group2','name7', 'name4','group7', ], 'group5' => [ 'name8' ] };

      Then, somehow, entires from column 2 (the groups' column) became keys in your 'hash table.' You need to discover how this happened, since the groupByCol2 subroutine merely uses your 'hash table's' values as keys and associates them with references to anonymous arrays which contain name entires.