Aloha Monks,

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;
mahalo, joseph w. guillaume

In reply to Opening files in a loop by loop362

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.