develooper Front page | perl.beginners | Postings from February 2002

Re: memory issues reading large files

Thread Previous | Thread Next
From:
Brett W. McCoy
Date:
February 7, 2002 13:55
Subject:
Re: memory issues reading large files
Message ID:
Pine.LNX.4.43.0202071659410.30587-100000@chapelperilous.net
On Thu, 7 Feb 2002, Brian Hayes wrote:

> It appears the problem was using the foreach statement instead of while.
> I have not tested this extensively, but using foreach the whole text
> file (or output of pipe) is read into memory before continuing, but
> using while (and probably for) each line is processed as it is read.

Yes indeed it does, because foreach operates on a list, where as while
operates on a boolean value returned from an expression (which means
scalar context).  So if you do

foreach(<FILE>) { }

It will put <FILE> into the list context and slurp the entire thing up
into a list.  'for' will do the same thing.  It also operates on a list
(foreach and for are aliases for each other).

-- Brett
                                          http://www.chapelperilous.net/
------------------------------------------------------------------------
As you grow older, you will still do foolish things, but you will do them
with much more enthusiasm.
		-- The Cowboy


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About