This comes from the fact that you open but never close your filehandle. $. is reset when the handle is closed, so in your case 0 is the first line of the first file, and perl counts up from that point as if you were reading one big file (and of course the $. < 12 bit only works once).

(Edit) You can either add an explicit close, or let perl do that automatically by having the handle only exist in the loop, which is done by moving the my-declaration inside the loop (this would be called "lexically scoping the handle to the loop"):

for my $i (2..12) { for my $k (5..18) { open my $handle, "<", "path" or die; # my means that the $handle +here is a new one on each call. ... # closed is called automatically here } }

By the way, instead of :

my @columns = split(/\s+/, $line); # split columns on whitespac +e my $col1 = shift @columns; # column 1 (throw away) my $col2 = shift @columns; # column 2 (throw away) my $col3 = shift @columns; # column 3 (special - keep this one) my $result = sprintf("%8.3f", $col3); # special format for col 3
You can either write: my (undef, undef, $first, @columns) = split(/\s+/, $line);
or (undef, undef, my $first, my @columns) = split(/\s+/, $line);,
whichever you find the easiest to read/understand. It means that the output of split will be placed in the elements of the list on the left side (called left values). Values that are sent to undef will be thrown away, $first is a scalar (you can read the $ as "single") and will take only one value, and since @columns is an array, it will take as many values as possible (all the rest).


In reply to Re^2: 'open for read' fails as "no such file or directory" but file is there by Eily
in thread 'open for read' fails as "no such file or directory" but file is there by fasoli

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.