ic23oluk has asked for the wisdom of the Perl Monks concerning the following question:
Hello perlmonks,
I am writing a program that checks a directory for FASTA (.fa) files (containing DNA sequences), opens them and prints out the names of the corresponding sequencing (characterised by '>' at the beginning of a line). However, my code cannot open the files and I don't know what the problem is.
Here's my code:
my $dir = "directory/"; opendir ( DH, $dir ) || die "Cannot open $dir: $!.\n"; foreach ( readdir DH) { if (/\.fa$/){ my $file = $_; open (READ, "$file") || die "Cannot open $file: $!.\n"; # her +e's the problem while (<READ>){ if ( /^>/){ print $_; } } } close (READ); } close (DH);
I could of course store each file name in a variable and open the files outside the loop, but I prefer a one-pass solution. Any ideas?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Cannot open file within a loop
by huck (Prior) on Jun 14, 2017 at 07:07 UTC | |
by ic23oluk (Sexton) on Jun 14, 2017 at 07:10 UTC | |
by huck (Prior) on Jun 14, 2017 at 07:36 UTC | |
by ic23oluk (Sexton) on Jun 14, 2017 at 07:41 UTC | |
|
Re: Cannot open file within a loop
by Discipulus (Canon) on Jun 14, 2017 at 07:40 UTC | |
|
Re: Cannot open file within a loop
by Marshall (Canon) on Jun 15, 2017 at 01:07 UTC |