http://www.perlmonks.org?node_id=22030


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

? ? ? ?

How did you come up with those reasons.

It is true that if you open a file for writing and the system crashes, the file is destroyed. But the file is opened in read/write. When the system crashes the file will be unchanged unless the file is closed properly. There may be other reasons to make a backup, you reason is not one of them.

As the foreach vs while, on my solaris there is no difference in memory usage between the two. I ran the two programs with a 40MB file and the memory usage was the same.

I can understand the file write vs read/write mistake, but next time could you please check you facts before you post.

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

Replies are listed 'Best First'.
RE: RE: RE: Re: How do I remove blank lines from text files?
by ZZamboni (Curate) on Jul 11, 2000 at 22:03 UTC
    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.

    --ZZamboni