in reply to Re: How to read in large files
in thread How to read in large files

Corion has correctly identified the issue, but I think a little more explanation might be helpful. When you use foreach, perl constructs the entire list before iterating over it. Using while, on the other hand, executes exactly 1 read attempt per loop since while executes after each true evaluation (successful read). In addition, the stored value goes out of scope at the end of each iteration, thus you only store 1 line at a time instead of all of them.


#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^3: How to read in large files
by davido (Cardinal) on Jan 28, 2016 at 16:42 UTC

    And this nuance occurs because:

    • In the case of foreach ( EXPRESSION ) { BLOCK }, the EXPRESSION is evaluated in list context. The <FILEHANDLE> operator returns a list of records from the file when evaluated in list context. Logical records are typically based on lines within the file.
    • In the case of while ( EXPRESSION ) { BLOCK }, the EXPRESSION is evaluated in scalar context for its Boolean value. The <FILEHANDLE> (diamond) operator returns a single record from the file when evaluated in scalar context.

    Dave

      Thanks all. I was unaware foreach was different from while other than syntactically.