CPAN: Perl Hashes sortieren

Beitrag lesen

my %traffic_map;  
foreach my $key_src_ip (keys %{$ip{"source"}}) {  
    foreach my $key_src_port (keys %{$ip{"source"}{$key_src_ip}}) {  
        foreach my $key_dest_ip (keys %{$ip{"source"}{$key_src_ip}{$key_src_port}}) {  
            foreach my $key_dest_port (keys %{$ip{"source"}{$key_src_ip}{$key_src_port}{$key_dest_ip}}) {  
                push @{$traffic_map{$ip{"source"}{$key_src_ip}{$key_src_port}{$key_dest_ip}{$key_dest_port}}},  
                    [$key_src_ip,$key_src_port,$key_dest_ip,$key_dest_port];  
            }  
        }  
    }  
}  
  
my $top = 0;  
for my $traffic (sort {$b <=> $a} keys %traffic_map) {  
    last if 2 == $top;  
    for my $src_dst (@{$traffic_map{$traffic}}) {  
        say sprintf '%20s:%-15s <-> %20s:%-15s %15d', @{$src_dst}, $traffic;  
        $top++;  
    }  
}  

Die zusätzliche Arrayreferenz ist dafür da, falls mehrere src_dst dieselbe Trafficzahl aufweisen.