You are accumulating 5 complete copies of your data in memory: @cvsdata, @sorted_data; the list you feed into map here map{ $_=join ", ", @{$_}}@sorted_dat;; the list you feed into join here: join "\n",map{; and the entire thing as single huge string here: my $temp = join "\n",.

By a slight rearrangement of your code you can avoid 3 of these:

... # now sort my @sorted_dat = sort tableSorter @csvdata; @csvdata = (); ## Discard the unsorted data ## csvify and output the sorted data one line at a time ## so avoiding two more in-memory copies # arrange the sorted array into cav format print $outfile join( ", ", @{$_} ), "\n" for @sorted_data; # close the output file close($outfile);

In theory, there is an optimisation in newer versions of Perl that will sort an array in-place if the output and input arrays are the same:

@csvdata = sort tableSorter @csvdata;

But it doesn't always seem to kick in?

Beyond that, you might need to resort to using an external sort utility (eg. your system's sort utility), though you might need to pre and/or post process your files to allow it to produce the sort order you need.


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.
RIP PCW It is as I've been saying!(Audio until 20090817)

In reply to Re: memory problems parsing large csv file by BrowserUk
in thread 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.