Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

RE: RE: RE: Re: How do I remove blank lines from text files?

by ZZamboni (Curate)
on Jul 11, 2000 at 22:03 UTC ( #22054=note: print w/replies, xml ) Need Help??

in reply to RE: RE: Re: How do I remove blank lines from text files?
in thread How do I remove blank lines from text files?

Wrt the file being opened read/write: if you are rewriting the whole file and it is a large file and the system crashes in the middle, I will argue that the file may be partly rewritten, depending on when the system buffers were last flushed. I don't think the file will remain unchanged until it is properly closed. But I'm willing to be convinced otherwise.

Wrt memory usage: my mistake. The immediate problem I saw with using foreach instead of while is that foreach provides an array context, whereas while provides a scalar context. Therefore, when you use foreach, the <FILE> slurps the entire file at once and creates a list for the foreach to cycle through. Then you are pushing each element into @data, from where I assumed the data would be duplicated. However, reading the foreach documentation, I see the following:

...each element of the list is aliased to the loop variable in turn ... Note that the loop variable becomes a reference to the element itself, rather than a copy of the element.
Therefore you will not be duplicating data. You are reading the whole file in memory only once, not twice as in my original message.

However, I still think reading the whole file in memory is a bad idea, because you will eventually find a file that will not fit in memory. Unless there are other reasons for doing it, I will always prefer to process it one line at a time.


  • Comment on RE: RE: RE: Re: How do I remove blank lines from text files?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://22054]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2020-10-26 21:28 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (254 votes). Check out past polls.