# The array @tag contains around 40000 records # Input_file.dat contains 30000 lines open(FH1,"+>Result_file.txt") or die "Cannot create file $!\n"; foreach my $fkey(@tag) { open(FH,") { if($_ =~ m/$fkey/g) { print FH1 "$_\n"; } } close(FH); } close(FH1);