操纵多维哈希?

时间:2018-02-06 01:08:55

标签: perl hash

我有一个脚本,它接收各种数据,并为我提供数据中每个功能关系的平均值。

while ( <IN> ) {
    chomp $_;

    if ($_ =~ m/Source/ ) {
        next;
    }
    else{

        @arr = split(/\t/, $_);

        $freq  = $arr[2];
        $term1 = $arr[4];
        $term2 = $arr[11];

        push ( @{ $hash{$term1}{$term2} }, $freq);
    }
}

## Gathers sum of all arrays for each function
foreach my $val ( keys %hash ) {

    foreach my $val2 ( keys %{ $hash{$val} } ) {

        my $count = sum (@{ $hash{$val}{$val2} });
        $total += $count;
    }
}

## calculates mean for each function
foreach my $val ( keys %hash ) {

    foreach my $val2 ( keys %{ $hash{$val} } ) {

        my $count = sum (@{ $hash{$val}{$val2} });
        my $avg   = $count/$total;

        print "$val\t$val2\t$avg\n";
    }
 }

示例输出

GIY-YIG catalytic domain        Unknown 0.24
Endonucelase VII        TEST    0.16
TEST    Endonucelase VII        0.16
Unknown GIY-YIG catalytic domain        0.12
Unknown Unknown 0.32

但我需要结合任何不是Unknown的反函数。

期望的输出

GIY-YIG catalytic domain        Unknown 0.24  <-stays the same
Endonucelase VII        TEST    0.32          <-changes
Unknown GIY-YIG catalytic domain        0.12  <--stays the same
Unknown Unknown 0.32

有没有简单的方法可以做到这一点?

1 个答案:

答案 0 :(得分:1)

my %data;
while (<>) {
   chomp;
   my ($term1, $term2, $freq) = split /\t/;
   if ($term1 eq "Unknown" || $term2 eq "Unknown") {
      say;
      next;
   }

   ($term1, $term2) = sort ($term1, $term2);
   $data{"$term1\t$term2"} += $freq;
}

say "$_\t$data{$_}" for keys %data;