- 论坛徽章:
- 307
|
本帖最后由 sunzhiguolu 于 2015-09-29 15:57 编辑
回复 1# baiguihuajl
您好, 看了您的代码. 发现您的问题属于代码逻辑问题:
问题代码范围: 19~25 行位置.
for($i=1;$i<=4;$i++){
open(F2,"$i.txt");
while(<F2>){
chomp;
@b=split(/\t/,$_);
foreach($b[0]){
if(exists $lines{$b[0]}){ #逻辑错误的地方
$lines{$b[0]}.="\t".$b[1];
}else{
$lines{$_}.="\t"."0";
}
}
}
close(F2);
}
红色:可以删除部分
- #添加的代码:
- sub gen_hash_by_file{
- my ($file, %h_file) = shift;
- open (my $fh_file, '<', $file);
- while (<$fh_file>){
- chomp;
- my ($id, $value) = split /\s+/;
- $h_file{$id} = $value;
- }
- close ($fh_file);
- %h_file;
- }
复制代码
- #修改后的代码: 14~28 行
- for (my $i = 1; $i <= 4; $i++ ){
- my %h_file = gen_hash_by_file($i . '.txt');
- foreach my $k (keys %lines){
- if (exists $h_file{$k}){
- $lines{$k} .= "\t" . $h_file{$k};
- }else{
- $lines{$k} .= "\t" . 0;
- }
- }
- }
复制代码 |
|