in reply to Re: Increment frequency of attempts based on IP and login details combination
in thread Increment frequency of attempts based on IP and login details combination
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.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"; } } } }
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 .....
|
|---|