Assuming that you are actually storing more than one value in the final array, and that you need to two level hash to do various lookups, the following approach should work for you.
First, collect all your data. Then create an array of key pairs. This array will provide the basis for any sorted orderings you may wish to create. Now you can sort the key pair array into whatever order you wish. You may want to look at the sort docs and sorting examples in perldsc for more help.
# Read in test data. while (<DATA>) { my ( $k1, $k2, $v ) = split; $hash{$k1}{$k2}[0] = $v } # Make an array of all key pairs. my @key_pairs; foreach my $k1 ( keys %hash ) { foreach my $k2 ( keys %{$hash{$k1}} ) { push @key_pairs, [$k1, $k2]; } } # Sort key pairs my @key_pairs_by_value = sort { $hash{ $b->[0] }{ $b->[1]}[0] <=> $hash{ $a->[0] }{ $a->[1]}[0] } @key_pairs; # print sorted for my $keys ( @key_pairs_by_value ){ my ($colour, $device) = @$keys; printf("\n%-55s %-55s %-40s", $colour, $device, $hash{$colour}{$device}[0] ); }
TGI says moo
In reply to Re: Sorting multi-hash values
by TGI
in thread Sorting multi-hash values
by papai
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |