in reply to One more problem
in thread While loop printing only one fasta header file
Try to only put <c> or <code> tags around the code itself and use <p> tags around your regular message. This makes your question more readable.
The logic problems in your code are centered on the range (..) operator. You can use Super Search to search the Tutorials for the "range operator" to learn more about it.
The range operator is a flip-flop, like a switch. It returns true when it's on, and false when it's off. Once the left hand side of the .. becomes true, the switch turns on. The "switch" will stay on (and keep returning true) unless it is triggered off. It is triggered off after the condition to the right of the .. becomes true. One thing to remember is it returns true that one time it is switching off (SEE TUTORIALS OK, hard to explain).
If your program had only one loop you probably wouldnt notice this behavior. With a nested loop, not so good. After a matching header is found in the line, the inner loop checks every other header. The range operator ("switch"/flip-flop/..) is still returning true and every time you check another header, the last line read into $_ is printed.
and here's my take:
#!/usr/bin/perl use warnings; # YOU FORGOT THESE! use strict; open HEADF, '<fastaheaders.txt' or die "cant open fastaheaders.txt: $!"; my @headers = <HEADF>; close HEADF; # create a regexp to search for all headers at once # like this: (BLABLABLA|ASKDJFASD) will match either # of the two. my $regexp = '(' . join('|', @headers) . ')'; print 'Enter filename: '; my $fasta_fn = <STDIN>; chomp $fasta_fn; # remove newline, paranoia open FASTAF, '<', $fasta_fn or die "can't open $fasta_fn: $!"; while(<FASTAF>) { # $_ = the line of text from the file now... # m!! is like using // to match text, m just tells # perl to use a different surrounding character: ! # m!$regexp!o by itself matches against $_ automagically! # (the o says only read the value in $regexp once, # it won't change anyway) # print by itself prints $_ automagically! print if(m!$regexp!o .. m!//! and not m!//!); } close FASTAF;
Untested! READ Tutorials or perlop for the m and .. operators and maybe perlrequick for regular expressions.
|
|---|