- 论坛徽章:
- 1
|
本帖最后由 wxlfh 于 2010-05-22 12:02 编辑
既然文件1和文件2的行数不一样,那么我可以猜想文件1的第一列肯定是唯一的,否则你的比较就没有意义了。那么,可以把文件1的第一列当散列的关键字,文件2的第2列在散列里查找是否有这个关键字。- use strict;
- my %file1;
- open my $fh1,"<","file1.txt" or die "Open file1.txt failed.\n";
- while (<$fh1>) {
- chomp;
- my @tmp = split /\s+/;
- $file1{$tmp[0]} = [@tmp[1..$#tmp]];
- }
- close $fh1;
- open my $fh2,"<","file2.txt" or die "Open file2.txt failed.\n";
- open my $out,">","file3.txt" or die "Create file3.txt failed.\n";
- while (<$fh2>) {
- chomp;
- my @tmp = split /\s+/;
- my $key = $tmp[1];
- if (exist $file1{$key}) {
- next if not ($file1{$key}->[0] <= $tmp[8]
- and $file1{$key}->[1] >= $tmp[9]);
- my @str = ($key,@{$file1{$key}},@tmp[0,2..$#tmp],"\n");
- print $out "@str";
- }else {
- next;
- }
- }
- close $fh2;
- close $out;
复制代码 |
|