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.


In reply to Re: Sort Hash of Hashes by key value by TomDLux
in thread Sort Hash of Hashes by key value by jgn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.