【发布时间】:2016-02-13 01:03:25
【问题描述】:
我的情况与 Stack Overflow 帖子类似Substituting array elements from one tab delimited file with hash values from another file using Perl。我正在尝试用特定列中的各自值替换与哈希键匹配的字符串。
Given the @array:
a b abbd
cc d abcd
gg hh cdag
and the %hash:
$VAR1 = {
'a' => 'GAT_1',
'b' => 'GAT_2',
'cc' => 'GAT_3',
'd' => 'GAT_4',
'gg' => 'GAT_5',
'hh' => 'GAT_6',
};
我已经尝试过这段代码,但它不起作用。如何限制仅替换匹配键的前两个实例(列)? (也就是保持第三列不变?)
foreach $line (@array) {
my @cols = split (/\s+/, $line);
$cols[0] = $hash{cols[0]};
$cols[1] = $hash{cols[1]};
push @newarray, $line;
}
Expected output:
GAT_1 GAT_2 abbd
GAT_3 GAT_4 abcd
GAT_5 GAT_6 cdag
【问题讨论】: