File namesmy $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);
Test file contentallergies immunizations
output:# 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>
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/.
produces this output: perl create_indivo_schemas.plopen (my $fh, "< $dir/$file" ) or die ("Can't open input file $!"); while ( < $fh > ){ print ; }
I also tried this:open (my $fh, "< input_files/immunizations" ) or die ("Can't open inp +ut file $!");
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.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"; }
In reply to reading files from a directory by kevyt
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |