Dear Monks, I can't seem to figure out how to sort on the 'priority' part of my HoH. Here's a short example of what I have tried:
#!/usr/bin/perl -w use strict; my %tmp = ( 'hostX1-1-l.example.com' => { 'priority' => { 'IS-ABC' => '3', }, 'os' => { 'linux' => 'linux', }, 'pager' => { 'man-duty' => 'man-duty', }, }, 'hostC1-3-l.example.com' => { 'priority' => { 'IS-XYZ' => '3', }, 'os' => { 'linux' => 'linux', }, 'pager' => { 'man-duty' => 'man-duty', }, }, 'hostc1-3-l.example.com' => { 'priority' => { 'OS-XYZ' => '3', }, 'os' => { 'linux' => 'linux', }, 'pager' => { 'man-duty' => 'man-duty', }, }, 'hostC1-9-l.example.com' => { 'priority' => { 'XS-XYZ' => '3', }, 'os' => { 'linux' => 'linux', }, 'pager' => { 'man-duty' => 'man-duty', }, }, 'hostH1-2-l.example.com' => { 'priority' => { 'IS-XYZ' => '3', }, 'os' => { 'linux' => 'linux', }, 'pager' => { 'man-duty' => 'man-duty', }, }, 'hostQ1-3-l.example.com' => { 'priority' => { 'IS-ABC' => '3', }, 'os' => { 'linux' => 'linux', }, 'pager' => { 'man-duty' => 'man-duty', }, }, ); sub sortBySite { my $site_a = $tmp{$a}{'priority'}; my $site_b = $tmp{$b}{'priority'}; return $site_a <=> $site_b; } #foreach my $host ( sort { $tmp{$a}{'priority'} <=> $tmp{$b}{'priority +'} } sort keys %tmp ) { #foreach my $host ( sort keys %tmp ) { foreach my $host ( sort sortBySite keys %tmp ) { for my $site ( keys %{ $tmp{$host}{'priority'}} ) { print "$host->$site is $tmp{$host}{'priority'}{$site}\ +n"; } }
The output I get is this:
hostX1-1-l.example.com->IS-ABC is 3 hostC1-3-l.example.com->IS-XYZ is 3 hostc1-3-l.example.com->OS-XYZ is 3 hostC1-9-l.example.com->XS-XYZ is 3 hostH1-2-l.example.com->IS-XYZ is 3 hostQ1-3-l.example.com->IS-ABC is 3
What I am try to get is this:
hostQ1-3-l.example.com->IS-ABC is 3 hostX1-1-l.example.com->IS-ABC is 3 hostC1-3-l.example.com->IS-XYZ is 3 hostH1-2-l.example.com->IS-XYZ is 3 hostc1-3-l.example.com->OS-XYZ is 3 hostC1-9-l.example.com->XS-XYZ is 3
Thanks

In reply to Sorting HoH on 2nd level key? by Anonymous Monk

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.