Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: No such file or directory at R_loop2.pl line 19.

by ig (Vicar)
on Aug 28, 2012 at 06:26 UTC ( [id://990146]=note: print w/replies, xml ) Need Help??


in reply to No such file or directory at R_loop2.pl line 19.

It may be that open fails when you try to access an array element that doesn't exist.

You initialize @lines with the contents of your file, then you iterate over the array, but you don't stop when you reach the end of the array: you keep going for two more iterations.

In scalar context (like when adding 1 and assigning to $count), @lines evaluates to the number of elements in the array. In your example: 11. Then you add 1, so $count is set to 12. You iterate while $k varies from 0 through 11 and stop when it reaches 12. But the indexes for your array are 0 through 10 - there is no element with index 11.

You could set count with $count = @lines - 1 or, even better $count = $#lines, but you don't use $k except to get elements from $lines, so you would be better to use a loop like:

foreach my $file (@lines) { open(my $fh, '<', $file) or die "$file: $!"; ... }

Another possibile cause of your fault is that you have line termination characters in your file names but you probably don't have files with names that end with line termination characters. You can use chomp to remove these characters.

my $file = "junk.txt"; open (my $fh, '<', $file) or die "Can't open $file for read: $!"; my @lines; while (<$fh>) { chomp; push (@lines, $_); } close($fh) or die "Cannot close $file: $!";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://990146]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-24 17:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found