If you have the memory something this will probably be faster. You can save on the if/else for every line as well as only doing the minimum in the loop (like not splicing and joining when we don't really need to). Even a saving of a few microseconds X millions of lines is substantial. Multiple calls to print are significantly slower than a single call as the OS can buffer/write more efficiently.

#!/usr/bin/perl my ($field,%fh); while ( <DATA> ) { @field = split /,/; $fh{$field[2]} .= "$field[0],$field[1],$field[3]"; } for my $file( keys %fh ) { open F, ">$file" or die $!; print F $fh{$file}; close F; }

cheers

tachyon


In reply to Re: Performance Trap - Opening/Closing Files Inside a Loop by tachyon
in thread Performance Trap - Opening/Closing Files Inside a Loop by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":