# create a new hash where the key is the value of the original hash, and the value is an array of keys from the original hash that have the given value foreach $key0 (keys(%data_hash)) { foreach $key1 (keys(%{$data_hash{$key0}})) { for ($i = 0; $i < @{$limit_hash{$key0}{$key1}{'limit'}}; $i++) { push(@{$new_hash{$data_hash{$key0}[$i]{$key1}}}, $key0 . "__" . $limit_hash{$key0}{$key1}{'limit'}[$i] . "__" . $key1); } } } # now print the new hash that is sorted by value foreach my $sorted_by_val ( sort{$a <=> $b} keys(%new_hash)) { printf("key %.2f: ", $sorted_by_val); foreach my $val (@{$new_hash{$sorted_by_val}}) { printf("$val"); } printf("\n"); }