Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

While loop stops after reading new line?

by JonathanPrice (Initiate)
on Oct 03, 2013 at 19:11 UTC ( #1056786=perlquestion: print w/replies, xml ) Need Help??
JonathanPrice has asked for the wisdom of the Perl Monks concerning the following question:

hello I am having a bit of an issue (i am new to perl) i have written a script that is supposed to read a FASTA file hopefully this will scroll through the file search for ">" and header (it does this). then save this line and the next line in two separate scalars. but it stops after reading the next line? does anyone know why this is? or how i can make it so that it carries on reading the file after finding the 1st correct sequence?? this is my code. thank you.

use strict; use warnings; my($filename) = "fasta.txt"; open(FILE, $filename); while(<FILE>) { my($line) =$_; if($line=~/^>/ and $line =~ /unknown/ ) { my($header) =$line; print $header; <FILE>; print $_; } } close FILE; exit;

Replies are listed 'Best First'.
Re: While loop stops after reading new line?
by toolic (Bishop) on Oct 03, 2013 at 19:28 UTC
    Maybe you want:
    $_ = <FILE>;

    • Post a few lines of your input file (less than 10 lines which demonstrate your problem).
    • Post your actual output.
    • Post your expected output.
Re: While loop stops after reading new line?
by CountZero (Bishop) on Oct 03, 2013 at 19:51 UTC
    I cleaned up your code and it now looks as follows:
    use strict; use warnings; my $filename = 'fasta.txt'; open my $FILE, '<', $filename or die "Could not open $filename: $!"; while(my $line = <$FILE>) { if($line=~/^>.*unknown/ ) { $line .= <DATA>; print $line; } } close $FILE;
    How do you know your script stopped reading after finding the first header? Are you sure your file contains more than one header?

    I ran the above on the following data:

    test not this > unknown print this no no no again > known (not this header) got you! no way Jose > HEADER unknown Yessir! not this line throw this away
    > unknown print this > HEADER unknown Yessir!


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: While loop stops after reading new line?
by Marshall (Abbot) on Oct 04, 2013 at 12:15 UTC
    For FASTA file format see:

    I would suggest a Perl module at:

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1056786]
Approved by toolic
Front-paged by toolic
and the grasshoppers chirp...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2018-05-25 21:26 GMT
Find Nodes?
    Voting Booth?