简体   繁体   English

Perl:在数组的 Hash 中合并重复的键

[英]Perl: Combine duplicated keys in Hash of Array

I having issues with this and wondering if someone could provide some help.我对此有疑问,想知道是否有人可以提供帮助。 I'm parsing a.txt file and want to combine duplicated keys and it's values.我正在解析一个 .txt 文件,并且想要组合重复的键和它的值。 Essentially, for each identifier I want to store it's height value.本质上,对于每个标识符,我想存储它的高度值。 Each "sample" has 2 entries (A & B).每个“样本”有 2 个条目(A 和 B)。 I have the file stored like this:我有这样存储的文件:

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];
     }
}

This seems to work fine, but my issue is that when I send this data to below function.这似乎工作正常,但我的问题是当我将此数据发送到 function 下方时。 It prints the $curr_identifier twice.它打印 $curr_identifier 两次。 I only want to populate unique identifiers and check for the presence of it's $height variable.我只想填充唯一标识符并检查它的 $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";

Basically, if sample height is present for both markers (A&B), then output is both markers.基本上,如果两个标记 (A&B) 都存在样本高度,那么 output 就是两个标记。

'1', 'A', 'B'

If height is not present, then output is empty for reported marker.如果高度不存在,则报告标记的 output 为空。

'2', 'A', ' '

'3', ' ', 'B'

My current output is printing out like this:我当前的 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

Your desired output can essentially be boiled down to these few lines of perl code:您想要的 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM