#!/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"; } }