I am opening a directory that has files that look like the following. Here is one file:
>UVWXY
ABCDEFGHIJKLMNOPQRSTUVWXYZ
>STUVW
ABCDEFGHIJKLMNOPQRSTUVWXYZ
>QRSTU
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Here is a second file:
>EFGHI
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Here is my code:
#!/usr/bin/perl
use warnings;
use strict;
my ($directory) = @ARGV;
my $dir = "$directory";
my @ArrayofFiles = glob "$dir/*";
open(OUT, ">", "/path/to/output.txt") or die $!;
foreach my $file(@ArrayofFiles){
open(my $fastas, $file) or die $!;
my $numberoffastas = grep{/>/}<$fastas>;
#print $numberoffastas, "\n";
while (my $line = <$fastas>){
print $line, "\n";
}
}
Nothing is printed out for $line, but this code correctly counts the number of ">"s that appear in the file when it is opened, evidenced by printing $numberoffastas.
How can I fix this code so that $line = something like:
>EFGHI
or
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Thanks
calls readline on the
$fastasfilehandle in list context, which consumes all the input on the filehandle. At your subsequent call inwhile (my $line = <$fastas>), there is no more input on that filehandle to provide, and thewhilecondition fails.Save the inputs in an array and perform both operations on the array
or if you are worried that the files are too large and will give you memory headaches, reopen the file
or
seekto the beginning of the file