- 論壇徽章:
- 0
|
本帖最后由 baiguihuajl 于 2015-12-11 15:19 編輯
有很多有兩列的txt文件,根據文件的第一列合并,之后每列根據每個txt文件寫,例如有3個txt文件,
1.txt:
a 2
b 3
c 4
2.txt:
b 1
c 3
d 2
e 12
3.txt:
c 2
d 1
e 2
f 1
合并成:
a 2 0 0
b 3 1 0
c 4 3 2
d 0 2 1
e 0 12 2
f 0 0 1
我的代碼是- for($i=1;$i<=3;$i++){
- open(F1,"$i.txt");
- while(<F1>){
- chomp;
- @a=split(/\t/,$_);
- $hash{$a[0]}=1;
- }
- close(F1);
- }
- for($i=1;$i<=3;$i++){
- open(F2,"$i.txt");
- while(<F2>){
- chomp;
- @b=split(/\t/,$_);
- $hash1{$b[0]}=$b[1];
- }
- close(F2);
- foreach(keys %hash){
- if(exists (keys %hash1){
- $hash{$_}.="\t".$hash1[$_];
- }else{
- $hash{$_}.="\t"."0";
- }
- }
- }
- foreach(keys %hash){
- print "$_\t$hash{$_}\n";
- }
復制代碼 代碼19行有問題,怎么改,想表示如果存在%hash1的鍵值,麻煩大家?guī)臀铱聪,感激不盡~~~ |
|