The problem with reading the entire file in at once is that a big file will
soak up all your memory--for no useful purpose.
I had a working script that was a while() loop that would read one line,
process it, throw it away, then read the next one. A contractor re-wrote
the script to suck the entire file into an array, THEN enter the same
line-at-a-time while() loop to do the processing. He, too, claimed that was
a more Perl-ish way.
After he left, we discovered that his machine--which he had had upgraded
with far more memory than any of the rest of us had--was the only one which
could run the script.
On the other hand, I have scripts that parse information out of financial
files. There is no possibly way to process those files line-at-a-time. You
must run up and down the file picking out pieces of information here and
there. In that case, I MUST read the entire file into an array--and it
works well.
So the moral to this story is to read the entire file into an array all at
once when you NEED random access to the lines. If your processing fits the
line-at-a-time model, when read a line at a time. The question about
"efficiency" is specious since the total read time in either method is
mainly governed by file buffering done behind your back and most of the
total run time will usually depend on the processing you do in the loop.
Merrill
> -----Original Message-----
> From: Flynn, Timothy J [SMTP:[EMAIL PROTECTED]]
> Sent: Tuesday, May 16, 2000 12:40 PM
> To: Perl-Win32-Users Mailing List
> Subject: Fastest / Best way to open files
>
> Hi,
>
> After reading the thread on (RE: verify file exits) I was curious.
> What is the fastest way to open a file in perl? A colleague of mine told
> me
> when I was learning perl that it is better to read the whole file in at
> once
> and store it in an array. I am currently using this:
>
> while (<FILE>) {
> push @FILE_FILE, [ split /whatever/, $_ ];
> }
>
>
> is there a better way? I just saw that you can simply do this:
>
> @FILE_FILE = <FILE>;
>
>
> Normally I would check to make sure that the line I read in actually had
> data, and clearly the = solution doesn't accommodate this unless you
> sliced
> the array afterwards. When I was learning C, C++ I always read a
> character
> at a time. So what is the best way in perl?
>
> Thanks!
> -Tim
>
> ---
> You are currently subscribed to perl-win32-users as:
> [EMAIL PROTECTED]
> To unsubscribe, forward this message to
> [EMAIL PROTECTED]
> For non-automated Mailing List support, send email to
> [EMAIL PROTECTED]
>
---
You are currently subscribed to perl-win32-users as: [archive@jab.org]
To unsubscribe, forward this message to
[EMAIL PROTECTED]
For non-automated Mailing List support, send email to
[EMAIL PROTECTED]