Hi monks I have written a script for sorting a csv file using parse::CSV. much of the info I got was off here so thanks: ) It works fine on reasonablly size csv files. But I have one that has 50 fields and approx 1400000 rows. I get the following memory related error. "out of memory during request for 4096 bytes, total sbrk() is 401864704 bytes" I was wondering of there was anyhting I could do about this basically? Can I dump info sporadically so the memory usage doesn't build up ? The code is
#!/bin/perl -w use Data::Dumper; use parse::CSV; my @csvdata; # open the file and parse my $file = Parse::CSV->new( file => 'darkint.csv', ); open($outfile, ">", 'sorted.csv') or die $!; while (my $val = $file->fetch) { push(@csvdata,$val); } if($file -> errstr){ print "$errstr error in parsing\n"; } # here we define a subroutine to sort our data in the correct manner sub tableSorter($$){ my ($row1,$row2) = @_; my $column10comparison = $row1->[9] cmp $row2->[9]; if($column10comparison != 0) { return $column10comparison; } my $column33comparison = $row1->[32] cmp $row2->[32]; if($column33comparison != 0) { return $column33comparison; } return $row1->[33] <=> $row2->[33]; } # now sort my @sorted_dat = sort tableSorter @csvdata; # arrange the sorted array into cav format my $temp = join "\n",map{ $_=join ", ", @{$_}}@sorted_dat; print $outfile $temp; # close the output file close($outfile);

In reply to memory problems parsing large csv file by turquoise_man

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.