loop362 has asked for the wisdom of the Perl Monks concerning the following question:
Still rather new but plugging away. My question is about opening files in a loop. Before this in the same script I opened a file that sucessfully opens the file that delivers the $data_names. I changed filehandle names, I'm not sure why. Can you keep opening and closing files with the same file handle? I think I'm missing something really simple. Why won't this file open?
for ($i=0; $i < $lines1; $i++) { print "Name $i is $data_names[$i]<p\/>"; open(FH, "<Files/$data_names[$i].txt")or die "No can! :$!"; print "Name $i is $data_names[$i]<p\/>"; @data = <FH>; print "Data for $data_names[$i] is $data[$i]<p\/>"; close (FH); }
#!/usr/bin/perl -Tw ##################################### # Script by Joseph W. Guillaume # # Last updated June 2, 2005 # ##################################### use strict; use CGI qw(:standard); print "Content-Type: text/html\n\n"; print '<body bgcolor="cccccc">'; ##################################### my $employee=param('employee_name'); my $certification=param('certification'); my $month=param('month'); my @month=""; my $day=param('day'); my $year=param('year'); my $mo_yr=param('mo_yr'); my @data; my @data_names; my $data_names; my $data; my $lines; my $lines1; my $number; my $i; my @mon = qw (January Feburary March April May June July August Septem +ber October November December); ##################################### # Read Names.txt file # ##################################### open(JOE, "<Files/Names.txt") or die "No can! :$!"; @data_names = <JOE>; #foreach (@data_names) { #close (JOE); #} print "Names are @data_names<p\/>"; $lines1 =scalar@data_names; print "Number of records in Database is $lines1 <HR><p\/>"; ##################################### # Read File # ##################################### #print "Employee <b> $employee </b>"; for ($i=0; $i < $lines1; $i++) { print "Name $i is $data_names[$i]<p\/>"; open(FH, "<Files/$data_names[$i].txt")or die "No can! :$!"; print "Name $i is $data_names[$i]<p\/>"; @data = <FH>; print "Data for $data_names[$i] is $data[$i]<p\/>"; close (FH); } ##################################### # Checking File # ##################################### $lines=scalar@data/5; if ($lines <= 0) { #print "has no records at this time."; print "<p\/><hr><p\/>"; while ($lines >=1) { #for ($i=1; $i<=$lines; $i++) { $certification = shift(@data); print "Certification is $certification on "; my $number = shift (@data); $month = $mon [$number-1]; print "$month"; $day = shift (@data); print " $day,"; $year = shift(@data); print " $year"; $mo_yr = shift(@data); #print " $mo_yr"; print "<p\/><HR>"; $lines--; } } ##################################### print '<div align="center"><table width="120" border="1" cellspacing=" +2" cellpadding="0"><tr><td align="center">To go Back</td></tr><tr><td + align="center"><a href="http://www.hawaiicivilmarriage.com/FFD/index +.html">Click Here</a></td></tr></table><p></p></div>'; print '</body'; ##################################### #exit;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Opening files in a loop
by chromatic (Archbishop) on Jun 04, 2005 at 02:13 UTC | |
by loop362 (Pilgrim) on Jun 04, 2005 at 02:24 UTC | |
|
Re: Opening files in a loop
by Zaxo (Archbishop) on Jun 04, 2005 at 02:19 UTC | |
by tilly (Archbishop) on Jun 04, 2005 at 03:15 UTC | |
|
Re: Opening files in a loop
by TedPride (Priest) on Jun 04, 2005 at 05:00 UTC | |
by chromatic (Archbishop) on Jun 04, 2005 at 05:53 UTC | |
|
Re: Opening files in a loop
by graff (Chancellor) on Jun 04, 2005 at 16:09 UTC | |
by loop362 (Pilgrim) on Jun 06, 2005 at 16:13 UTC |