【问题标题】:Dynamically build a HoHoA动态构建 HoHoA
【发布时间】:2013-09-15 10:56:30
【问题描述】:

我正在尝试将大量数据组织到数组哈希的哈希中。当我手动声明值等时,以下工作正常:

#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;


my %experiment = (
    'gene1' =>  {
                       'condition1' => ['XLOC_000157', '90', '0.001'],
                       'condition2' => ['XLOC_000347','80', '0.5'],
                       'condition3' => ['XLOC_000100', '50', '0.2']
                   },
    'gene2'   =>  {
                       'condition1' => ['XLOC_025437', '100', '0.018'],
                       'condition2' => ['XLOC_000322', '77', '0.22'],
                       'condition3' => ['XLOC_001000', '43', '0.002']

                   }
);

然后打印出键/值:

for my $gene (sort keys %experiment) {
    for my $condition ( sort keys %{$experiment{$gene}} ) {
        print "$gene\t$condition\t";
            for my $values (@{$experiment{$gene}{$condition}} ) {
                print "[$values]\t";
            }
        print "\n";
     }
}

输出:

gene1   condition1  [XLOC_000157]   [90]    [0.001] 
gene1   condition2  [XLOC_000347]   [80]    [0.5]   
gene1   condition3  [XLOC_000100]   [50]    [0.2]   
gene2   condition1  [XLOC_025437]   [100]   [0.018] 
gene2   condition2  [XLOC_000322]   [77]    [0.22]  
gene2   condition3  [XLOC_001000]   [43]    [0.002] 

但是,我正在处理的真实数据太大而无法手动声明,因此我希望能够达到与上述相同的结果,但是从包含每个字段的数组开始,例如:

示例输入:

condition1    XLOC_000157    1.04564    0.999592      99.66   gene1
condition1    XLOC_000159    0.890436    0.999592    99.47   gene2
condition2    XLOC_000561    -1.05905    0.999592      91.57   gene1
condition2    XLOC_00076    -0.755473    0.999592      99.04   gene2

将输入拆分为数组:

my (@gene, @condition, @percent_id, @Xloc, @change, @q_value @split, %experiment);
while (<$list>) {
    chomp;
    @split = split('\t');
    push @condition, $split[0];
    push @Xloc, $split[1];
    push @change, $split[2];
    push @q_value, $split[3];
    push @percent_id, $split[4];
    push @gene, $split[5];
}   

我一直在构建 HoA 以这样存储它:

push @{$results{$gene_name[$_]} }, [ $Xloc[$_], $change, $q_value, $percent_id[$_] ] for 0 .. $#gene_name;

但我现在正在尝试为每个 HoA 整合“条件”信息,从而构建一个 HoHoA。理想情况下,我想以与上述类似的方式在 while 循环中(因此“动态地”)执行此操作,以实现以下数据结构:

$VAR1 = {
          'gene1' => {
                       'condition1' => [
                                         'XLOC_000157',
                                         '1.04564',
                                         '0.999592',
                                         '99.66'
                                       ],
                       'condition2' => [
                                         'XLOC_000561',
                                         '-1.05905',
                                         '0.999592'
                                         '91.57'

                                       ],

                     },
          'gene2' => {
                       'condition1' => [
                                         'XLOC_000159',
                                         '0.890436',
                                         '0.999592'
                                         '99.47'

                                       ],
                       'condition2' => [
                                         'XLOC_00076',
                                         '-0.755473',
                                         '0.999592'
                                         '99.04'

                                       ],

                     }
        };

【问题讨论】:

  • 好的,你想像哈希一样构建%experiment吗?输入数据如何?
  • 你是从文件中读取输入数据吗??
  • 我已将这个问题重新阅读了三遍,但仍然不明白您在追求什么。你在多大程度上手动声明了这个数据结构?您想动态生成整个 HoHoA 还是只是避免输入 @arrays 的内容?我建议您查看有关如何扩展自己的复杂数据结构的规范参考:perldoc perldsc
  • 这个问题让我想起了我的第一个 SO 问题:stackoverflow.com/q/1089530/133939。从那以后我学到了很多东西,这让我感到惊讶!
  • 手动我的意思是按字面意思输入键名等。对于上面的示例,每个键/值都将存储在一个数组中 - 我将修改问题以反映这一点...

标签: arrays perl data-structures hash


【解决方案1】:
my %experiment;
while (<$list>) {
    chomp;
    my ($condition, $xloc, $percent_id, $gene) = split /\t/;
    $experiment{$gene}{$condition} = [ $xloc, $percent_id ];
}

【讨论】:

    猜你喜欢
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 2013-10-21
    • 2013-05-11
    • 2012-11-23
    • 2021-11-29
    相关资源
    最近更新 更多