Arrays 动态构建HoHoA
我正试图将一堆数据组织成数组散列。当我手动声明值等时,以下操作很好:Arrays 动态构建HoHoA,arrays,perl,data-structures,hash,Arrays,Perl,Data Structures,Hash,我正试图将一堆数据组织成数组散列。当我手动声明值等时,以下操作很好: #!/usr/bin/perl use warnings; use strict; use Data::Dumper; $Data::Dumper::Sortkeys = 1; my %experiment = ( 'gene1' => { 'condition1' => ['XLOC_000157', '90', '0.001'],
#!/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整合“条件”信息,从而构建一个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'
],
}
};
my%实验;
而(){
咀嚼;
我的($condition,$xloc,$percent_id,$gene)=split/\t/;
$experiment{$gene}{$condition}=[$xloc,$percent_id];
}
好的,你想像散列一样构建%实验
?输入数据看起来怎么样?你正在从文件中读取输入数据吗?这个问题我已经读了三遍,仍然不明白你在找什么。您手动声明此数据结构的程度如何?您想动态生成整个HoHoA,还是避免键入@array
s的内容?我建议您看看关于如何扩展自己的复杂数据结构的规范参考:这个问题让您想起了我的第一个问题:。从那以后,我学到了多少东西,这使我至今感到惊讶!对于上面的示例,每个键/值都将存储在一个数组中-我将修改这个问题以反映这一点。。。
$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'
],
}
};
my %experiment;
while (<$list>) {
chomp;
my ($condition, $xloc, $percent_id, $gene) = split /\t/;
$experiment{$gene}{$condition} = [ $xloc, $percent_id ];
}