If you never have anything besides "tcp" under "ip1" (or "ip2", etc), then "tcp" represents an unnecessary layer in the structure -- based on the sample data you show, the top-level keys could just as well be "ip1.tcp", "ip2.tcp", etc.
Anyway, something like this would produce the output format you suggested:
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} );
}
}
}
Note how the curly braces are placed around
$host{$ip} and then also around
$host{$ip}{$proto} so that these hash values (which happen to be a references to another hash) can be dereferenced for use with the "keys" function -- the "%" outside of those exta curlies tells perl to pass the entire dereferenced hash to "keys".
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.