my $regex = '(?:' . join('|', @id) . ')'; while ( $data = ) { print OUT $data if $data =~ /$regex/o; }