There are two parts to the puzzle - how to sort a large file, and how to merge multiple large sorted files. Super Search will help with the first part. The second part is achieved by reading a record from each file then outputting and replacing the smallest record in the current set until all records have been processed. Something like:

use strict; use warnings; my $data1 = <<DATA; 1 3 7 10 29 DATA my $data2 = <<DATA; 2 4 11 20 22 24 30 DATA my $data3 = <<DATA; 90 DATA my @files; # Open files and add them to the list for my $inFile (\$data1, \$data2, \$data3) { push @files, []; open $files[-1][0], '<', $inFile; my $fh = $files[-1][0]; $files[-1][1] = <$fh>; if (!defined $files[-1][1]) { close $files[-1][0]; pop @files; next; } chomp $files[-1][1]; } while (@files) { my $minRecIndex = 0; # Find the current minimum record for my $cmpRecIndex (0 .. $#files) { $minRecIndex = $cmpRecIndex if $files[$cmpRecIndex][1] < $files[$minRecIndex][1]; } print "$files[$minRecIndex][1]\n"; my $fh = $files[$minRecIndex][0]; $files[$minRecIndex][1] = <$fh>; if (defined $files[$minRecIndex][1]) { chomp $files[$minRecIndex][1]; } else { close $files[$minRecIndex][0]; splice @files, $minRecIndex, 1; } }

Prints:

1 2 3 4 7 10 11 20 22 24 29 30 90

Perl reduces RSI - it saves typing

In reply to Re: how to read multiple file and sort the lines by GrandFather
in thread how to read multiple file and sort the lines by newPerlr

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.