Just another way of iterating on a deeply nested hash which does not need to go back to the original hash each time (a made up example)
use strict; use warnings; use Data::Dumper; my %hash; my $count = 0; # populating a deeply nested hash for my $ip (qw <ip1 ip2 ip3>) { for my $port ( qw <port1 port2 port3>) { for my $status (qw <stat1 stat2>) { for my $user (qw <user1 user2 user3>) { $hash{$ip}{$port}{$status}{$user} = $count++; # (just + a dummy value for testing) } } } } # print Dumper \%hash; # iterating through the nested hash for my $nest0 (values %hash) { while (my ($port, $nest1) = each %$nest0) { while (my ($status, $nest2) = each %$nest1) { for my $freq (values %$nest2) { print "$port\t$status\t$freq\n"; } } } }
Note: I use each when I need both the key and the sub-hash ref (because I need to print the key) and values when I only need to dive one step further into the nested structure. But, for some reason, a for loop with each seems to behave inconsistently (incomplete results) on my Perl version, so I used a while loop for the cases with each. I think to remember from the past there was a bug at some point on each, maybe my version of Perl is one of them. Well, anyway, replacing the for loop by a while loop solves the issue.

This prints in part:

port2 stat2 9 port2 stat2 11 port2 stat2 10 port2 stat1 6 port2 stat1 8 port2 stat1 7 port3 stat2 15 port3 stat2 17 port3 stat2 16 port3 stat1 12 port3 stat1 14 port3 stat1 13 port1 stat2 3 port1 stat2 5 port1 stat2 4 port1 stat1 0 port1 stat1 2 .....

In reply to Re^2: Increment frequency of attempts based on IP and login details combination by Laurent_R
in thread Increment frequency of attempts based on IP and login details combination by firepro20

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.