in reply to split large CSV file >9.1MB into files of equal size that can be opened in excel
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.)
|
|---|