in reply to Re: 'open for read' fails as "no such file or directory" but file is there
in thread 'open for read' fails as "no such file or directory" but file is there
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 :
You can either write: my (undef, undef, $first, @columns) = split(/\s+/, $line);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
|
|---|