use strict; #a good habit to start your prog use warnings; use Bio::SeqIO; #load your codes.txt into an array open(my $fh, "<","codes.txt") or die("could not open codes.txt $!"); #a more proper way to opening files my @codes=<$fh>; #slurping codes.txt into an array my $obj=Bio::SeqIO->new( #Bio::SeqIO for input -file=>"fasta.fa", -format=>"fasta" ); my $out_obj=Bio::SeqIO->new( #Bio::SeqIO for output -file=>">matching.fa", -format=>"fasta" ); while(my $seq=$obj->next_seq){ foreach my $element (@codes){ chomp $element; if($seq->id=~/$element*?/gi){ #regular-expression based matching $out_obj->write_seq($seq); #output written to matching.fa } } }