The two main ways to process a file are 1) slurp it into an array, and 2)
read it line by line.
Option 1 offers the niceties of an array, where you can randomly access any
line in the file using array notation. The downside is that it requires the
entire file's contents to be loaded into memory. This problem is negligible
for small files, but for files of a significant size it can slow your
program down considerably.
open FILE, $myfile or die "$myfile: $!";
@filecontents = <FILE>;
close FILE;
#process @filecontents
Option 2 avoids the memory problems of the first option by only loading a
single line at a time. If you want to randomly access the file, though, it
will take a little more work (seek, etc). However, if your data is
structured that each line is processable on its own, then this is a fairly
fast method.
open FILE, $myfile or die "$myfile: $!";
while (<FILE>) {
# process $_
}
close FILE;
The less known Option 3 is to read the entire file into a scalar value and
do your parsing with a split. I have seen benchmarks on the reading of the
file but not on the splitting of it. For reading, it is the fastest method
(IIRC).
open FILE, $myfile or die "$myfile: $!";
read FILE, $filecontents, -s FILE;
close FILE;
while (split (/\n/, $filecontents)) {
# process $_
}
I don't know if the speed advantage of the read is offset by the split.
Benchmarking them would reveal the speed differences.
Lauren
> -----Original Message-----
> From: Flynn, Timothy J [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, May 16, 2000 10:40 AM
> 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]