in reply to Question on updating a hash table
You're relatively close, but remember hashes can only have one value per key name. What you want is to push the new hash reference into an array under the top level key name. Here's an example:
while (<DATA>){ my @ju = split /\s+/, $_; push @{ $self->{code}{$ju[0]} }, { event => $ju[1], month => $ju[2], am => $ju[3], pm => $ju[4], }; } print Dumper $self->{code}; __END__ # trimmed down example output $VAR1 = { 'RF858' => [ { 'am' => '7.22', 'event' => 'U888', 'pm' => '4.20', 'month' => 'Dec' } ], 'VS524' => [ { 'am' => '5.94', 'month' => 'Jan', 'pm' => '9.46', 'event' => 'U509' }, { 'am' => '5.22', 'month' => 'Aug', 'pm' => '1.78', 'event' => 'U14' } ], };
Here's one way that describes how to access all portions of the struct
for my $top_key (keys %{ $self->{code} }){ print "$top_key\n"; for my $href (@{ $self->{code}{$top_key} }){ for (keys %$href){ print "\t$_ => $href->{$_}\n"; } print "\n"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Question on updating a hash table
by TJ-47 (Initiate) on Mar 25, 2016 at 23:16 UTC |