I ran the OP code on a random CSV with 5-80 characters on each line, 1M lines (about 44MiB total): it behaved as the OP described as the desired output: all the files were ~10k each, with the final file being the leftover lines. With reasonable sized lines, I could not replicate the failure where one file (specifically, the second) ended up hugely bigger than the others. I played with changing the chunk sizes, and it always did what I expected.

While typing up that paragraph, I realized: what if one line were hugely longer than all the others? If one line is significantly larger than all the others (my new test data has line#1023 being 1M characters, rather than ≤80 characters), then when the 10KB read finished for the OP's second output file, it then read the remainder of that huge line using the $chunk = <FH>; in the "second if", making the second file huge. By removing the "second if" (presumably the whole if-block, not just the if-statement like I originally interpreted), then, like stevieb said, the complete line was not read, and the files all ended up the same size.

And, as advice to the OP, based on recommendations I've often seen in the Monastery: use the 3-argument form of open, use lexical filehandles (my $fh and my $out instead of FH and OUT; Super Search for "lexical filehandles" to see some explanations as to why), consider autodie instead of manually adding the or die "..." to each (in a larger program, you might forget it on one or more calls to open... but bonus points for actually doing the check in this program.)


In reply to Re: split large CSV file >9.1MB into files of equal size that can be opened in excel by pryrt
in thread split large CSV file >9.1MB into files of equal size that can be opened in excel by Anonymous Monk

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.