I preferred , in this instance , to work with an array, and to check the contents of the array. This is because further down the track of this program I want to manipulate the contents of some lines in the array, and add additional lines to the arrays.

It might still be worth considering doing the work in one pass while reading the file line-by-line. While 5174 lines is probably still small enough to fit into memory, if your file does grow, performance might suffer, and you might end up having to rewrite the program. The general approach would be to use a while (<$filehandle>) loop to read the file line-by-line and keep track of the current line number yourself or with Perl's special variable $.. For each line you read, either modify it or don't, and print it back out. When you hit the line number where you want to insert records, use additional print statements in an if to do so.

But of course I also understand the simplicity of just manipulating an array. Note that the core module Tie::File might be worth looking at, it will give you a normal-looking Perl array, but behind the scenes it is actually accessing the lines of the file, while keeping only some of them in memory. You can then do all your normal Perl array operations on it, including splice to remove and insert records, and the changes will be made to the file. While the module isn't necessarily the fastest when it comes to writing to the file, it does have the advantage to give you a pretty transparent interface and you don't have to worry about memory management.


In reply to Re^3: Array size too big? by haukex
in thread Array size too big? by JillB

Title:
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, details, 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, summary, 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.