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.


In reply to Re: One more problem by juster
in thread While loop printing only one fasta header file by ashnator

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.