Hi Conal.

I can't imagine what you're trying to solve at the moment, and your link doesn't make it clearer - to me anyway. I suspect you want to sort a hash alphabetically and then perhaps upon various parts of it's value.

I tried your code:

my %server_hash = ( foo => {tag => 1}, bar => {tag => 10}, boot => {tag => 2}, snow => {tag => 8}, fish => {tag => 3}, adv => {tag => 1} ); foreach my $server ( sort {$server_hash{$a} cmp $server_hash{$b} && $server_hash{$a}{tag} <=> $server_hash{$b}{tag} } keys %server_hash) { print "$server:$server_hash{$server}{tag}\n"; }
and it gave me:
foo:1 adv:1 boot:2 fish:3 snow:8 bar:10
So I don't think that that's really what you're trying to do. Remember that you won't have several keys in the hash having the same value. Hashes don't do that.

How about you give us a sample hash? And the desired result: a list of sorted keys? If you do just want to sort the hash alphabetically, then you're almost there.

Jarich


In reply to Re: hash sort problems by jarich
in thread hash sort problems by Conal

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.