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
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |