PDA

View Full Version : Read entire file or one at a time?


Tony Davis
07-08-2005, 08:36 PM
I have always read files into an array as a whole (as in the following code). I am not sure I know how to do it any other way. Is the general consensus that this is less efficient than reading one record at a time (assuming that the file is, let's say, between 1,000 and 10,000 records). And by the way, the file is a .txt file.

open(HANDLE, $data_file);
open(DAT, $data_file) || die("Could not open file, $data_file");
@rawdata=<DAT>;
close(DAT);

Thanks,
-tdavis

joeframbach
07-08-2005, 09:19 PM
i like to use a while loop.

open (FIN, $filename) or die $!;
while (<FIN>) {
#do stuff with one record.
}

mlseim
07-08-2005, 09:49 PM
I found this ... but don't know if it's good information or not.

http://www.cbs.dtu.dk/courses/27613/tips.html

Tony Davis
07-11-2005, 02:47 PM
Thanks! That is exactly what I am looking for. I think that I do many things in Perl that are not exactly "the best" if you know what I mean. I am a programmer, but not a Perl programmer on a day-to-day basis, so I am always looking for good feedback.

Thanks again!
-tdavis