Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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;

Comment on While loop stops after reading new line?
Download Code
Re: While loop stops after reading new line?
by toolic (Chancellor) 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
    Output:
    > unknown print this > HEADER unknown Yessir!

    CountZero

    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 (Prior) on Oct 04, 2013 at 12:15 UTC
    For FASTA file format see: http://en.wikipedia.org/wiki/FASTA_format#Format

    I would suggest a Perl module at: http://www.bioperl.org/wiki/Main_Page

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1056786]
Approved by toolic
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2014-08-30 11:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls