- 论坛徽章:
- 6
|
2楼主意不错,要是a开头的也有几个G咋整。。。
建议把文件打散成小文件,到系统能一次读进去的大小,排序每一个文件。然后两两比较合并输出。
分享个合并的想法:{:3_193:}- sub merge_sorted_file {
- my ($file1, $file2, $output_file) = @_;
- my $fh1;
- my $fh2;
- my $output_fh;
- my $smallest_so_far = q{};
- use autodie qw{open close};
- open $fh1, '<', $file1;
- open $fh2, '<', $file2;
- open $output_fh, '>>', $output_file;
- my $line_f1 = <$fh1>;
- my $line_f2 = <$fh2>;
- while (1) {
- if ($line_f1 le $line_f2) {
- $smallest_so_far = $line_f1;
- $line_f1 = <$fh1> if not eof $fh1;
- }
- else {
- $smallest_so_far = $line_f2;
- $line_f2 = <$fh2> if not eof $fh2;
- }
- print {$output_fh} $smallest_so_far;
- last if eof $fh1 and eof $fh2;
- }
- close $fh1;
- close $fh2;
- close $output_fh;
- }
复制代码 |
|