[英]Perl: Combine duplicated keys in Hash of Array
我对此有疑问,想知道是否有人可以提供帮助。 我正在解析一个 .txt 文件,并且想要组合重复的键和它的值。 本质上,对于每个标识符,我想存储它的高度值。 每个“样本”有 2 个条目(A 和 B)。 我有这样存储的文件:
while(...){
@data= split ("\t", $line);
$curr_identifier= $data[0];
$markername= $data[1];
$position1= $data[2];
$height= $data[4];
if ($line >0){
$result[0] = $markername;
$result[1] = $position1;
$result[2] = $height;
$result[3] = $curr_identifier;
$data{$curr_identifier}= [@result];
}
}
这似乎工作正常,但我的问题是当我将此数据发送到 function 下方时。 它打印 $curr_identifier 两次。 我只想填充唯一标识符并检查它的 $height 变量是否存在。
if (!defined $data{$curr_identifier}[2]){
$output1= "no height for both markers- failed";
} else {
if ($data{$curr_identifier}[2] eq " ") {
$output1 = $markername;
}
}
print $curr_identifier, $output1 . "\t" . $output1 . "\n";
基本上,如果两个标记 (A&B) 都存在样本高度,那么 output 就是两个标记。
'1', 'A', 'B'
如果高度不存在,则报告标记的 output 为空。
'2', 'A', ' '
'3', ' ', 'B'
我当前的 output 打印如下:
1, A
1, B
2, A
2, ' '
3, ' '
3, B'
_DATA_
Name Marker Position1 Height Time
1 A A 6246 0.9706
1 B B 3237 0.9706
2 A 0
2 B B 5495 0.9775
3 A A 11254 0.9694
3 B 0
您想要的 output 基本上可以归结为这几行 perl 代码:
while (<DATA>) {
($name,$mark,$pos,$heig,$time) = split /\t/;
print "'$name','$mark','$pos'\n";
}
__DATA__
... your tab-separated data here ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.