my %with_key; foreach my $first_key (keys %all_files) { foreach my $second key (keys %{$all_files{$first_key}}) { push @{$with_key{$second_key}}, $first_key; } } #### my %total; foreach my $subhash (values %all_files) { foreach my $key (keys %$subhash) { $total{$key} += $subhash->{$key}; } }