in reply to ?? Blazes under Linux; crawls under XP & Win2K...
Don't open and close the output files for every line. Open them at the start of the run, and close them at the end of your program (optional). Also, error checking is always a good idea:
my $reportfile = '/home/user/data.txt'; open my $reports, "<", $reportfile or die "Could not read '$reportfile': $!"; my @knowntypes = qw(1 2 3 4 5); my %filehandle; for my $type (@knowntypes) { open $filehandle{$type}, ">>", "form_${type}_record.txt" or die "Couldn't create 'form_$type_record.txt': $!"; }; for (<$reports>) { /^(\d+)\|/ or die "Malformed input line: $_"; my $type = $1; if (not exists $filehandle{$type}) { die "Unknown record type $type in line $_"; }; print $filehandle{$type} $_; };
If you hit the file descriptor limit of your OSes (255 on Win32, some other number on Linux), there is a module for that, FileCache. The Perl Cookbook has a recipe detailing its use (7.17).
Update: ChemBoy spotted a typo. "form_${type}_record.txt" instead of "form_$type_record.txt". That's why we use strict;.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: ?? Blazes under Linux; crawls under XP & Win2K...
by tweetiepooh (Hermit) on Jan 09, 2006 at 13:44 UTC | |
|
Re^2: ?? Blazes under Linux; crawls under XP & Win2K...
by Anonymous Monk on Jan 09, 2006 at 19:35 UTC | |
|
Re^2: ?? Blazes under Linux; crawls under XP & Win2K...
by WordWeaver (Acolyte) on Jan 13, 2006 at 10:36 UTC |