I know each $id in @ids does occur in one of the files but in my code the result of print is always empty!The quickest thing to try is to chomp your @ids:
my @ids =<IDLIST>; chomp @ids;
You should not assume that one approach is faster than another; Benchmark it instead. Avoiding unix grep should make your code more portable, but you would have to measure whether it is faster with or without unix grep.
Also, I think you are unnecessarily looping through all your files multiple times. It seems there is no need for your foreach my $fasta (@files) { loop.
Here is a slightly more conventional approach, in my opinion, which does not use Perl's grep:
foreach my $id (@ids) { foreach my $fasta (@files) { open my $fh, '<', $fasta or die "can not open file $fasta: $!" +; while (<$fh>) { print if /$id/; } close $fh; } }
In reply to Re: faster way to grep
by toolic
in thread faster way to grep
by coldy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |