- 论坛徽章:
- 0
|
本帖最后由 aef25u 于 2011-03-27 09:33 编辑
注:one.txt的数据为楼主贴出的第一份数据。
注意:使用本代码时,请做好two.txt数据的备份,因本代码会往two.txt覆盖写入新数据。- use Tie::File;
- use Fcntl 'O_RDONLY';
- #one.txt read-only :mode => O_RDONLY
- tie @arrOne, 'Tie::File', 'one.txt' ,autochomp => 1,mode => O_RDONLY or die "Problem tying @array: $!";
- tie @arrTwo, 'Tie::File', 'two.txt' ,autochomp => 1 or die "Problem tying @array: $!";
- my %One_3col,my %One_2col;
- foreach my $strOne (@arrOne){
- my @oneArr=split("[ \t]+",$strOne);
- $One_2col{$oneArr[0]}=$oneArr[1];
- $One_3col{$oneArr[0]}=$oneArr[2];
- }
- my %hashSum,%hashNum,%hashAvg;
- foreach my $strTwo (@arrTwo){
- my @twoArr=split("[ \t]+",$strTwo);
- $hashSum{$twoArr[1]}+=$twoArr[5];
- $hashNum{$twoArr[1]}++;
- $hashAvg{$twoArr[1]}=int($hashSum{$twoArr[1]}/$hashNum{$twoArr[1]});
- }
- my %hasResult,$subNum;
- foreach my $key (keys %hashAvg){
- if (exists $One_3col{$key}){
- $subNum=$One_3col{$key}-$hashAvg{$key};
- }
- $hasResult{$key}="+ $key $One_2col{$key} $hashAvg{$key} $subNum $hashNum{$key}";
- }
- @arrTwo=();
- my $i=0;
- for my $key (keys %hasResult){
- $arrTwo[$i]=$hasResult{$key};
- $i++;
- }
- untie @arrOne;
- untie @arrTwo;
复制代码 |
|