Opening and closing a file for each record is going to get you lousy performance. If the records are sorted by filename, try (untested):
Otherwise, sort by filename (untested):my $openfile = ''; foreach my $file (@file) { my $filename = $file->{filename}; my $content = $file->{content}; if ($filename ne $openfile) { $openfile = $filename; open(FILE, "> $filename") or die "Can't open $filename $!"; } print FILE $content; } close(FILE) if $openfile;
You may get slightly better results saving all the records for a file in an array and then printing them together just before opening the next file (or the close at the end).for my $file (sort {$a->{filename} cmp $b->{filename}} @files) { ... }
If you had a smaller number of files, I'd recommend a hash of filehandles instead:
foreach my $file (@files) { my $filename ... my $content ... if (!$fh{filename}) { open $fh{filename}, "> $filename" or die ... } print {$fh{filename}} $content; } foreach my $fh (keys %fh) { close($fh{$fh}); }
In reply to Re: Quickest way to write multiple files
by ysth
in thread Quickest way to write multiple files
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |