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.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re: Removing Large Invoices from a Data File by BrowserUk
in thread Removing Large Invoices from a Data File by JayDog

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.