galeb abu-ali wrote:
Hi,

Hello,

I'm trying to create a genbank library file that contains several genbank
records. I read in the genbank record names from a separate file into an
array and then loop through array of file names, open each file and read
contents into another array. The problem is in looping through the array and
opening individual files. The code is below. I successfully populate @ids
but cannot manage to loop through @ids, open files and populate @library as
I get "can't open file !\n"; messages. Please advise at your convenience,
Many thanks,

galeb

#!/usr/bin/perl
# create_gb_library.pl
use strict; use warnings;

open IDFILE, shift or die "can't read idfile!\n";

my @ids = '';

Why are you assigning '' to $ids[0]?


while(<IDFILE>  ) {
     my $filename = $_;

That is usually written as:

while ( my $filename = <IDFILE> ) {

You should also use chomp here.

    chomp $filename;


     push( @ids, $filename );
}

Perhaps you should just do that like this instead:

open IDFILE, '<', shift or die "can't read idfile because: $!";

chomp( my @ids = <IDFILE> );


my @library;

for my $id( @ids ) {
     chomp $id;
     open FILE, $id or die "can't open file $id!\n";

The first entry in @ids is '', which you assigned yourself, and which can never be a valid file name, hence the error message "can't open file !\n". Notice the space between 'file' and '!'.

Do the file names you get use absolute paths or relative paths? Is your program's current working directory the place where these files exist or are they in another directory? Do you need to prepend a directory name in order to access them?



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to