in reply to Removing Large Invoices from a Data File
One problem with your existing code is that you are counting the lines $outsize++;, not the bytes, and comparing that line count against your chunksize if( $outsize>$chunksize ..., which means your 'small' files are going to end up containing 500,000 lines! If your lines average 67 byte per line, then each output file would be around 33MB.
You need to change that to be $outsize += length() + 1; to achieve your original aim.
Update: I noticed the $outsize++; at the top, but not the $outsize += length at the bottom of the loop. Why two statements?
Beyond that, to achieve your second aim, you will need to buffer the contents of each file and accumulate a second per file count of the bytes read, and delay writing until you have accumulated a complete invoice.
Only at that point will you be able to determine whether to write the buffer out to the current composite file, or to a separate file, dependant upon how big it is. Remember to subtract the accumulated per file count from $outsize when you write individual files. Or only accumulate to $outsize when you have written to the composite output file.
It would also save time if you tested for the start-of-invoice condition using substr, eg.
if( substr( $_, 67, 2 ) eq '11' ) { ...
It gets expensive running up the regex engine on every line of files of this size.
|
|---|