my $regex = join '|', map quotemeta, @ids; while () { $counts{$1}++ if /($regex)/; }