for my $ip ( sort keys %host ) { print "$ip\n"; for my $proto ( sort keys %{$host{$ip}} ) { # assuming there will be keys other than "tcp" for my $port ( sort {$a<=>$b} keys %{$host{$ip}{$proto}} ) { printf( "%3d\t%s\t%s\n", $port, $host{$ip}{$proto}{$port}{state}, $host{$ip}{$proto}{$port}{service} ); } } }