lener has asked for the wisdom of the Perl Monks concerning the following question:
What I'd like to do is print it out while sorting by 'cores' (descendingly) first, 'memory' (descendingly) second and nodenames (ascendingly) last. So sort by cores, and if the number of cores is equal then sort by memory, if both are equal then sort lexicographically by the node names. It's supposed to look like this:$free_space = { "node1" => { "cores" => 12, "mem" => 200 }, "node2" => { "cores" => 12, "mem" => 500 }, ... }
I've tried doing it "manually" by creating a new HoHoA, where the 'cores' values are first-level keys, 'memory' values are second-level keys and the array holds the names of the nodes, then printing it simply by sorting level by level, like this:cores memory node ----- ------ ---- 12 400 node456 12 400 node534 12 350 node23 11 500 node12 11 200 node3 10 900 node10
Is there a more efficient solution?my %CoreMemNodes = (); #HoA <free_cores> -> <free_mem> -> (node1,node2 +,..) foreach my $node (keys %free_space) { push @{ $CoreMemNodes{$free_space{$node}{'cores'}}{$free_space{$no +de}{'mem'}} }, $node; } foreach my $free_cores (sort { $b <=> $a } keys %CoreMemNodes) { foreach my $free_mem (sort { $b <=> $a } keys %{ $CoreMemNodes{$fr +ee_cores} }) { foreach my $node (sort @{ $CoreMemNodes{$free_cores}{$free_mem +} }) { print "SORTED:$free_cores $free_mem $node\n"; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: sort HoH by second level values in specific order
by 1nickt (Canon) on Dec 15, 2017 at 15:45 UTC | |
by lener (Initiate) on Dec 15, 2017 at 17:51 UTC | |
|
Re: sort HoH by second level values in specific order
by LanX (Saint) on Dec 15, 2017 at 15:25 UTC | |
by lener (Initiate) on Dec 15, 2017 at 17:52 UTC | |
|
Re: sort HoH by second level values in specific order
by poj (Abbot) on Dec 15, 2017 at 15:50 UTC | |
|
Re: sort HoH by second level values in specific order
by choroba (Cardinal) on Dec 16, 2017 at 10:43 UTC | |
|
Re: sort HoH by second level values in specific order
by Laurent_R (Canon) on Dec 15, 2017 at 20:43 UTC | |
|
Re: sort HoH by second level values in specific order
by Anonymous Monk on Dec 16, 2017 at 01:02 UTC |