in reply to write to/append a specific line in output file

This is just a thought, but I think that you could use the Tie::File module for this purpose.

You could tie the output file to an array, and then use something like this:

$array[2] .= 'B';

This will append 'B' to the second line of the output file. The documentation states, that the file is not loaded into memory, so this will work even for gigantic files.

regards,
Luke Jefferson

Replies are listed 'Best First'.
Re^2: write to/append a specific line in output file
by jwkrahn (Abbot) on Aug 18, 2011 at 20:26 UTC
    $array[2] .= 'B';

    This will append 'B' to the second line of the output file.

    $array[2] is actually the third line of the file.    Array indexes start at 0.

      You are right, but it could be different if the module ties the file to the array starting at its second index.

      The module's documentation gives mixed information. My suggestion was based on the following quote from module synopsis:

      $array[13] = 'blah'; # line 13 of the file is now 'blah'

      Same assumption is repeated through the pod file (line 17 under index 17 and so on). But in another place, the same documentation states:

      The first line of the file is element 0 of the array; the second line +is element 1, and so on.

      I think it's best that the OP tries the module, and does not assume anything, prior to some tests.


      UPDATE:

      jwkrahn is right. Out of curiosity, I did the tests - all works like a normal array should - $array[2] points to the third line of the file. Seems that the module documentation could use some corrections.

      regards,
      Luke Jefferson

Re^2: write to/append a specific line in output file
by aquinom (Sexton) on Aug 18, 2011 at 20:03 UTC
    Thanks, I think that's what I was looking for.