Pack the data into a big vector and then sort it inplace using Sort::Packed. Memory requirements will get reduced to ~1/4 of your data set.
#!/usr/bin/perl use strict; use warnings; use Sort::Packed qw(sort_packed); my $packed = ''; my ($len); my %val = (A => 0, C => 1, G => 2, T => 3); my %rev = reverse %val; sub compress { my @data = split //, scalar(reverse shift); my $out = ''; for (my $i = 0; $i < @data; $i++) { my $bits = $val{$data[$i]}; defined $bits or die "bad data"; vec($out, $i, 2) = $bits; } scalar reverse $out } sub decompress { my $data = reverse shift; my $len = shift; my $out; for (my $i = 0; $i< $len; $i++) { $out .= $rev{vec($data, $i, 2)} } scalar reverse $out; } while(<DATA>) { chomp; ($len ||= length) == length or die "bad data"; $packed .= compress $_; } my $bytes = int(($len * 2 + 7) / 8); my $n = length($packed) / $bytes; sort_packed "C$bytes" => $packed; for (my $i = 0; $i < $n; $i++) { print decompress(substr($packed, $i * $bytes, $bytes), $len), "\n" +; } __DATA__ AAACGAGAAGTAATATCAGTATCGTATGCTTCAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA TTTTCGCCTTCCGGGTGCCGCTGGGGTCTTTCTC GCACACTCGGAGCCCGGGGAGCCAGAGGAAACAA GATCATGACACAGTTGATAAAATTGTTGTTCAGA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACC AAACGAGAAGTAATATCAGTATCGTATGCTTCAC AAACGAGAAGTAATATCAGTATCGTATGCTTCGA AAACGAGAAGTAATATCAGTATCGTATGTTTCAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAATA
Preallocating the vector will also further reduce memory comsumption.

In reply to Re: Sorting Gigabytes of Strings Without Storing Them by salva
in thread Sorting Gigabytes of Strings Without Storing Them by neversaint

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.