Neat. Only very rarely have I actually cared about perl's memory usage and probably all five times it's been for a very large sorting job. (I define large as being four times the size of memory, in this case two one gig text files). I've often found Knuth's The Art of Computer Programming vol 2, Sorting and Searching to be an excellent source of ideas when the common sort() isn't good enough. I recommend it as an excellent resource for ideas.

For actual solutions I normally look for things like a disk-base db file (usually BerkeleyDB) for an external sort or some variation on partitioning and/or radix sort. My favorite and least effort trick when sorting voting records from the Secretary of State (by precinct and by voter id) just involves doing a combination of a radix and external sort: find a key field (I choose voting precinct) that can partition the data into appropriate sized bins and do just insert into the appropriate files (one per precinct).

Once all the files are complete I do an in-memory plain-jane sort() on the data and write it back out over the previously unsorted partition. At this point I can either stop and decide to access the data from the individual partitions as-is or just cat(1) the files together and recreate a full (approximately 1 gig) text file. That's only a scetch but there are plenty of other great ideas in just the sorting section of TAoCP. It's worth checking out.

Pre-publication update: broquaint pointed me to the excellent node resorting to sorting which does a nice overview of some common sorts.

__SIG__ use B; printf "You are here %08x\n", unpack "L!", unpack "P4", pack "L!", B::svref_2object(sub{})->OUTSIDE;

In reply to Re: An APL trick for the Schwartzian Transform by diotalevi
in thread An APL trick for the Schwartzian Transform by dingus

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.