[英]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.