Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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: $!";

In reply to Re: No such file or directory at line 19. by ig
in thread No such file or directory at line 19. by zakishah

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (10)
    As of 2018-06-21 20:50 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (119 votes). Check out past polls.