in reply to 'open for read' fails as "no such file or directory" but file is there

A different strange thing happens now.

If I run the script for each $i individually, for example ($i=1; $i<=1 $i++) or ($i=2; $i<=2; $i++) etc, everything is correct and fine. If I run it for $i from 1 to 5 , ($i=1; $i<=5; $i++) , I get the following errors, starting when the script has finished running $i=1, so from $i=2 onwards:

Argument "Angle:" isn't numeric in sprintf at auto-dash.pl line 51, <$ +input> line 200035. Argument "RB-A1=-1.46"" isn't numeric in sprintf at auto-dash.pl line +51, <$input> line 200035. Argument "label" isn't numeric in sprintf at auto-dash.pl line 47, <$i +nput> line 200036. Argument ""Time" isn't numeric in sprintf at auto-dash.pl line 51, <$i +nput> line 200036. Argument "(ps)"" isn't numeric in sprintf at auto-dash.pl line 51, <$i +nput> line 200036. Argument "label" isn't numeric in sprintf at auto-dash.pl line 47, <$i +nput> line 200037. Argument ""Angle" isn't numeric in sprintf at auto-dash.pl line 51, <$ +input> line 200037. etc *more same lines that go up to <$input> line 400064*

Also these arguments that it's finding as wrong belong in the first 12 lines of each input file, that it's meant to skip.

My inputs have only 100001 lines, so for it to say that it's finding errors on line 200000 is by itself strange as there is no such line. This tells me it's somehow creating (??) an input with more lines? Does that again have to do with how I use the open command?

Please let me know if I've managed to explain this properly or not as I would appreciate any advice on how to solve this :'(

Replies are listed 'Best First'.
Re^2: 'open for read' fails as "no such file or directory" but file is there
by Eily (Monsignor) on Nov 02, 2016 at 17:28 UTC

    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).