##构建哈希算法 my (%h, %abc, $k, $v,$guo,$gang,$snei,$sji,$shi,%hash,$key); open (FH,") or die "$!"; while (<FH>) { chomp; if (/^(200\d+)\s+(\d{12})\s+(\S+)\s+(\S+)\s+(\d{8})\s+(\S+)\s+(\d+)\s+(\d+)\s+(\S+)/) { my @tmp = split /\s+/,$_; my $k=$tmp[0]."\t".$tmp[4]; $h{$k}=[] if !defined $h{$k}; push @{$h{$k}},$tmp[5], $tmp[8]; } } close FH; ####################################################################### ##分析处理哈希数据结构,得出结果(未排序) open (FILE,">log_out.txt") or die "$!"; while(($k, $v) = each %h) { %abc=@$v; $guo = $abc{'国际长途费'}?$abc{'国际长途费'}:0; $gang = $abc{'港澳台长途费'}?$abc{'港澳台长途费'}:0; $snei = $abc{'省内长途费'}?$abc{'省内长途费'}:0; $sji = $abc{'省际长途费'}?$abc{'省际长途费'}:0; $shi = $abc{'市话通话费'}?$abc{'市话通话费'}:0;
print FILE "$k\t$guo\t$gang\t$snei\t$sji\t$shi\n"; } close FILE; ######################################################################## ##对上面结果按升幂排序(按月份+电话号码) open (QQ,") or die "$!"; open (HH,">final_out.txt") or die "$!"; print HH "月份\t电话号码\t国际\t港澳台\t省内\t省际\t市话\n"; print HH "-" x 70,"\n"; while (<QQ>) { chomp; if (/(\d{6})\s+(\d{8})\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/) { $hash{"$1\t$2"} = "$3\t$4\t$5\t$6\t$7"; } } foreach my $key(sort keys %hash) { print HH "$key\t$hash{$key}\n"; } close QQ; close HH; ######################################################################## ##删除过渡文本log_out.txt
|