in reply to Sort Hash of Hashes by key value
Seems to me you're going to want the server name in your output, too.
The trivial way to arrange that is to stick another key-value pair server => 'foobar47' into each set. But wh6y have a top level hash, in the first place? ... unless you're doing things by name basis, elsewhere, just make it a top-level array:
push @servers, { server => 'foobar47', pctused => $pctused, volname => $volname, volfree => $volfree } if $pctused > $LIMIT;
You don't need to put variables between quote to have them interpolated, you just want the value. So now you can sort the list based on the pctused.
my @sorted = sort { $a->{pctused} <=> $b->{pctused} } @servers;
The other way is to use a Schwartzian Transform. Extract the keys from the original data structure; use that to form pairs where the first element is what you want to sort by, and the second element is the true key. Sort by the first elements. Then extract the true keys, i.e. the server names, sorted by the otehr element.
It looks kind of scary if you're new to Perl. The key concept is that map takes an array on the right, whose elements are processed one by one in the block which follows the 'map' keyword, where it's known as '$_'
my @sorted_server_names = map { $_->[1] } sort { $a->[0] <=> $b-><[0] map { [ $srvlist{$_}{pctused}, $_ ] } keys %srvlist
As Occam said: Entia non sunt multiplicanda praeter necessitatem.
|
|---|