in reply to Confused on handling merging values for hash of hashes
# WRONG %HOH = [ ... ];
If key is always unique, why it's not the hash key? Read the comments to understand the steps I'd take:
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my @AoH = ( { key => 1, person => "Mike", possession => "wallet, keys, car, house", age => 25, }, { key => 2, person => "Mike", possession => "dog, cat, baseball bat", age => 25, }, { key => 3, person => "Dave", possession => "pony, house, car, keys", age => 21, }, ); # Make "key" the hash key: my %HoH; for my $hash (@AoH) { my $key = delete $hash->{key}; $HoH{$key} = $hash; } print Dumper \%HoH; # In fact, using an array for the possessions would be even better. for my $hash (@AoH) { $hash->{possession} = [ split /, /, $hash->{possession} ]; } print Dumper \@AoH; # You don't need any key. You want to hash by person and age. Using pu +sh merges Mike's possessions. my %HoH2; for my $hash (@AoH) { push @{ $HoH2{ $hash->{person} }{ $hash->{age} } }, @{ $hash->{pos +session} }; } print Dumper \%HoH2;
|
|---|