while (my $in = ){ foreach (@ref){ if ($_=~ m/$in/){ print OUT "$_"; }}}