2014-11-07 54 views
1

我有以下腳本,使用它可以比較使用哈希的兩個文件的列。perl使用具有多個密鑰的哈希比較文件

但是,當$轉換cols [5]和$ table cols [2]匹配時,我想要打印出$ conversion中另一列的值,即cols [1]中的相應值。我試圖通過將cols [1]的值賦給我的%散列中的第二個鍵(稱爲$ keyfield2)來做到這一點。但是我沒有成功打印它。這是我的代碼到目前爲止:

my %hash =(); 
while(<$conversion>){ 
    chomp; 
    my @cols = split(/\t/); 
    my $keyfield = $cols[5]; 
    my $keyfield2 = $cols[1]; 
    $hash{$keyfield,$keyfield2}++; 
    } 
seek $table,0,0; #cursor resetting 
while(<$table>){ 
    my @cols = split(/\t/); 
    my $keyfield = $cols[2]; 
    if (exists($hash{$keyfield})){ 
     print $output "$cols[0]","\t","$hash{$keyfield2}","\t","$cols[1]\n"; 
    } 
} 

任何提示如何做到這一點?

回答

2

是否有你使用哈希引用的原因。隨着哈希試試這個:

my $keyfield = $cols[5]; 
my $keyfield2 = $cols[1]; 
$hash{$keyfield} = $keyfield2 

,並打印到:

print $output "$cols[0]","\t","$hash{$keyfield}","\t","$cols[1]\n"; 
+1

山坳被覆蓋掉在第二循環中。所以在打印中col是來自表格的值。只需使用$ keyfield2即可從轉換中獲取col1的值。 – 2014-11-07 02:29:26