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

Re: No such file or directory at line 19.

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

in reply to No such file or directory at 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://990146]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2018-06-17 22:33 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (107 votes). Check out past polls.