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

Re: memory issues reading large files

Thread Previous | Thread Next
Brett W. McCoy
February 7, 2002 13:55
Re: memory issues reading large files
Message ID:
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
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 Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About