The first example never makes sense in the given form. If you are processing stuff sequentially, from beginning to end, it makes sense to use the file iterator instead of slurping it into memory, as by that way, you will not use as much system memory and perls buffering will handle the reading from the file in a relatively efficient manner.

As soon as you need to go back and forth between lines, and jump backwards in the files, then the first approach of reading the whole file into memory makes the algorithm much easier to write, at the cost of some system memory. If system memory is scarce and you are able to trade a bit of time against memory, take a look at Tie::File, which lets you access a file on disk just like an array in memory.


In reply to Re: Slurping file into array VS Process file line-by-line by Corion
in thread Slurping file into array VS Process file line-by-line by monkfan

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.