in reply to Sorting a HoH by values of the nested hashes
Hi
There must be a better way, but this the best idea I could come up quickly from the top of my mind, and it works:
Output:use strict; use warnings; my @array; my $VAR1 = { '1.2.3.4' => { '27029' => 5, '20617' => 1 }, '1.2.3.5' => { '51509' => 1, '17427' => 1, '63961' => 7, '22331' => 1, '20780' => 7 }, '1.2.3.6' => { '1500' => 3 }, + }; for my $var1 (keys %$VAR1) { push @array, [$var1, $_, $$VAR1{$var1}{$_}] for keys %{$$VAR1{$var +1}}; } print map {$_->[0], "\t", $_->[1], "\t", $_->[2], "times \n"} sort {$a +->[2] <=> $b->[2]} @array;
1.2.3.5 51509 1times 1.2.3.5 17427 1times 1.2.3.5 22331 1times 1.2.3.4 20617 1times 1.2.3.6 1500 3times 1.2.3.4 27029 5times 1.2.3.5 63961 7times 1.2.3.5 20780 7times
Update This is strange, there was no answer when I posted my answer, and now it appears that two monks actually posted before me. I do not understand this and would probably have not posted the above if I has seen the prevoous answers.
|
|---|