my %hashes_for; # More sensible name while (){ next unless /something/; if (//){ push @{$hashes_for{$3}}, { sig => $1, src => $3, proto => $2, dst => $4, port => $5, }; } } # You can now use scalar(@{$hashes_for{whatever}}) instead of $count{whatever}