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;.
In reply to Re: ?? Blazes under Linux; crawls under XP & Win2K...
by Corion
in thread ?? Blazes under Linux; crawls under XP & Win2K...
by WordWeaver
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |