Hi Monks, I have a perl script that almost does what I want but not quite. I'm trying to divide a CVS file into multiple files of equal size. What I have divides almost all of the files into ones that are equal sizes but always leaves one larger file. I'm guessing it's the first or last file that's written in the series. If you could have a look at the code below and tell me what I'm doing wrong/have missed I'd greatly appreciate it. Thanks .

#!/usr/bin/perl -w use strict; use warnings; #split large files mkdir "split_files"; open (FH, "all_tags2.csv") or die "Could not open source file. $!"; my $i = 0; while (1) { my $chunk; print "process part $i\n"; open(OUT, ">split_files/all_tags2$i.csv") or die "Could not open d +estination file"; $i ++; if (!eof(FH)) { read(FH, $chunk, 10000); print OUT $chunk; } if (!eof(FH)) { $chunk = <FH>; print OUT $chunk; } close(OUT); last if eof(FH); }

In reply to 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.