my %seen; while(){ foreach my $i (@newips){ print OUTFILE if /\;drop;[^"]*$i/ && ++$seen{$i} <= 500; } }