Hello Monks,

I need help with a complex (well for me anyway) sort routine. I have a hash that has various elements, and 3 of those elements are the region, country, and city. Right now I am sorting on the region, but I also need to sort on the country next, and city last.

So, for the following data:
APAC India Delhi APAC China Suzhou APAC Australia Sydney APAC Japan Tokyo EMEA SouthAfrica Johannesburg EMEA Russia Moscow APAC China Shanghai EMEA Germany Munich EMEA France Paris EMEA Italy Venice NLAM Argentina BuenosAires2 NLAM USA NYC NLAM Brazil San Paulo NLAM USA Austin EMEA Italy Rome NLAM USA Boston
I need it to be sorted as such:
APAC Australia Sydney APAC China Shanghai APAC China Suzhou APAC India Delhi APAC Japan Tokyo EMEA France Paris EMEA Germany Munich EMEA Italy Rome EMEA Italy Venice EMEA Russia Moscow EMEA SouthAfrica Johannesburg NLAM Argentina BuenosAires2 NLAM Brazil San Paulo NLAM USA Austin NLAM USA Boston NLAM USA NYC
Here is where I am extracting my hash and how I sort based on region
sub print_stats{ my $subinput = $_[0]; for my $site1 (keys %siteInfo){ #Site has to be in correct region next unless ($siteInfo{$site1}{region} eq $subinput); print "$siteInfo{$site1}{region},$siteInfo{$site1}{country},$site1, $siteInfo{$site1}{totalDevices}, ,$siteInfo{$site1}{totalVulns}, $siteInfo{$site1}{totalS5s},"; } } }
Thanks,
Dru

Perl, the Leatherman of Programming languages. - qazwart

In reply to Sort a Hash 3 Levels Deep by Dru

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.