I have always had problems reading files from a directory and I hope someone can help. I think the problem is in the the "/" in the open command.
my $dir = 'input_files';
opendir(DIR, $dir) or die "can't opendir $!";
while (defined(my $file = readdir(DIR))) { # do something with "$d
+irname/$file"
print "The directory and file are $dir/$file\n";
open (IN, "< $dir/$file" ) or die ("Can't open input file $!");
while ( < IN > ){
print ;
}
}
closedir(DIR);
File names
allergies
immunizations
Test file content
# Input file for allergies
# Date, Diagnosed By, Type, allergy, reaction,specifics<br>
2009-05-16,Children's Hospital Boston,drugs,penicillin,Blue rash,This
+only happens on weekends<br>
2009-05-17,Boston Medical Group,drugs,Vitamin B,Rash on torso,This hap
+pens after 9PM<br>
2009-05-17,Memorial Hospital,food,Diary,Upset stomach and gas, Happens
+ after drinking whole milk<br>
output:
perl create_indivo_schemas.pl
Name "main::IN" used only once: possible typo at create_indivo_schemas
+.pl line 18.
The directory and file are input_files/allergies
INThe directory and file are input_files/immunizations
INThe directory and file are input_files/..
INThe directory and file are input_files/.
open (my $fh, "< $dir/$file" ) or die ("Can't open input file $!");
while ( < $fh > ){
print ;
}
produces this output:
perl create_indivo_schemas.pl
The directory and file are input_files/allergies
GLOB(0x8dbaa14)The directory and file are
input_files/immunizations
GLOB(0x8dd4214)The directory and file are input_files/..
GLOB(0x8e33a2c)The directory and file are input_files/.
This also will not work
open (my $fh, "< input_files/immunizations" ) or die ("Can't open inp
+ut file $!");
I also tried this:
my @file_list = `ls input_files`; # get the list of files from the i
+nput directory
foreach (@file_list){ # loop through the files that are in the direct
+ory
chomp;
my $filename = 'input_files/'.$_;
print "file name is : $filename\n";
open (my $fh, "< $filename" ) or die ("Can't open input file $!");
open (my $fh, "< $filename" ) or die ("Can't open input file $!");
while ( < $fh > ){ # look through the data in the file
print;
print "\n";
}
Can someone tell me what's wrong? I think I had a problem with this a year ago but I was able to get it to work on windows. So, that makes me think it's not the code.
Thanks
My font on this post seems a bit large. Sorry.