Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
The output I get is this:#!/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"; } }
What I am try to 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
ThankshostQ1-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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sorting HoH on 2nd level key?
by pc88mxer (Vicar) on Jul 24, 2008 at 03:55 UTC | |
by Anonymous Monk on Jul 24, 2008 at 04:10 UTC |