while () { my ($source, $sport, $to, $dport, $proto, $packs, $bytes) = split; my $key = "$source$dport"; if ( exists( $total{$key}{from} )) { $total{$key}{connects}++; $total{$key}{bytes} += $bytes; } else { $total{$key} = { from => $source, to => "$to:$dport", bytes => $bytes, }; # maybe should set 'connects => 1' as well? } $total += $bytes; }