I am writing a simple script that I want to read in an array of input file names, then loop through each file ... performing some operations and generating an output file for each corresponding input file. I have a script that is "close" to working (at least I believe it's close).

When attempting to execute, the script gets through the first iteration ($k=0, code attached below) just fine, generating the output file "ND_out-02-00.txt", but as it increments to $k=1, I get the following error:

"print() on closed filehandle OUT at isotopes-p7.perl line 17, <GEN0> line 46079. Use of uninitialized value in scalar chomp at isotopes-p7.perl line 19, <GEN0> line 46079. Use of uninitialized value in concatenation (.) or string at isotopes-p7.perl line 20, <GEN0> line 46079. Can't read the source:No such file or directory at isotopes-p7.perl line 20, <GEN0> line 46079."

#!/usr/local/bin/perl -w use IO::File; my $file = IO::File->new; @FN=("out-02-00.txt","out-02-01.txt","out-02-02.txt","out-02-03.txt"," +out-02-04.txt","out-02-05.txt","out-02-06.txt","out-02-07.txt","out-0 +2-08.txt","out-02-09.txt", "out-02-10.txt","out-02-11.txt","out-02-12.txt","out-02-13.txt", +"out-02-14.txt","out-02-15.txt","out-02-16.txt","out-02-17.txt","out- +02-18.txt", "out-02-20.txt","out-02-21.txt","out-02-22.txt","out-02-23.txt", +"out-02-24.txt","out-02-25.txt","out-02-26.txt","out-02-27.txt","out- +02-28.txt", "out-02-30.txt","out-02-31.txt","out-02-32.txt","out-02-33.txt", +"out-02-34.txt","out-02-35.txt","out-02-36.txt","out-02-37.txt","out- +02-38.txt"); $FN=@FN; for($j=0; $j<$FN; $j++){ print "$FN[$j]\n"; } for($k=0; $k<$FN; $k++){ print "$FN[$k]\n"; $filename = <$FN[$k]>; chomp ($filename); $file->open("< $filename") or die("Can't read the source:$!"); open(OUT, ">ND_$filename"); select (OUT); @BU=(); @MA1=(); @MA2=(); until ($file->eof) { my $line = $file->getline(); if ($line =~ /K-INF,LEAK \(B2/) { my @col3 = split(qr/\s+/s, $line); #split on whitespace push(@BU,"$col3[14]"); } elsif($line =~ /0 EID: 93237/) { $line = $file->getline(); chomp($line); @col1 = split(qr/\s+/s, $line); push(@MA1,"$col1[3] $col1[4] $col1[5] $col1[6] $col1[7] $col +1[8] $col1[9] $col1[10] $col1[11]"); } elsif ($line =~ /0 EID: 95241/) { $line = $file->getline(); chomp($line); @col2 = split(qr/\s+/s, $line); push(@MA2,"$col2[3] $col2[4] $col2[5] $col2[6] $col2[7] $col +2[8] $col2[9]"); } } # end of until for($i=1; $i<=58; $i++){ print "$BU[$i-1] $MA1[$i-1] $MA2[$i-1]\n"; } close(OUT); }
I know the file names are correct, and in the directory and that they are being accessed in that name sequence since my first for loop simply tests that the names are as expected. I think my problem may be the recursive open/close statements for either the input or output files (or both). Anybody know what's going on here? Thanks! ~Jack

In reply to Open in for loop/array structure by igotlongestname

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.