我想为每个$ COMMUNITY从0开始增加哈希值,我从0定义一个数组@indicator直到数组@NAME的相同索引,然后将其推入值,但这不是我想要的确切地说,我不知道该怎么做。我很清楚该值不是按顺序排列的,因为数组中的位置不是首先组织的,而是如何准确地执行此操作,然后, 如何打印subhash $ FAMILY的每个第一个值。 例如,
community 0 = name 0 4 7
community 1 = name 0 3 8
community 2 = name 0 3 6
#!/usr/bin/perl
use warnings;
use strict;
use Tie::Autotie 'Tie::IxHash';
my @NAME= qw(AA AB AC BA BB BC CA CB CC AA AB AC BA BB BC CA CB CC AA AB AC BA BB BC CA CB CC AD CD CE CF BD BE);
my @FAMILY= qw(A A A B B B C C C A A A B B B C C C A A A B B B C C C A C C C B B);
my @COMMUNITY= qw(0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 0 2 2 2 1 1);
tie my %COMMUNITY_FAMILY_NAME,'Tie::IxHash' ;
my @indicator;
foreach (my $x=0;$x<=scalar @NAME;$x++)
{
push @indicator,$x;
}
push @{ $COMMUNITY_FAMILY_NAME{ $COMMUNITY[$_] }{ $FAMILY[$_]}{$NAME[$_]} }, $indicator[$_] for 0 .. $#NAME;
print Dumper(\%COMMUNITY_FAMILY_NAME);
输出:
$VAR1 = {
'0' => {
'A' => {
'AA' => [
0
],
'AB' => [
1
],
'AC' => [
2
],
'AD' => [
27
]
},
'B' => {
'BA' => [
3
],
'BB' => [
4
],
'BC' => [
5
]
},
'C' => {
'CA' => [
6
],
'CB' => [
7
],
'CC' => [
8
]
}
},
'1' => {
'A' => {
'AA' => [
9
],
'AB' => [
10
],
'AC' => [
11
]
},
'B' => {
'BA' => [
12
],
'BB' => [
13
],
'BC' => [
14
],
'BD' => [
31
],
'BE' => [
32
]
},
'C' => {
'CA' => [
15
],
'CB' => [
16
],
'CC' => [
17
]
}
},
'2' => {
'A' => {
'AA' => [
18
],
'AB' => [
19
],
'AC' => [
20
]
},
'B' => {
'BA' => [
21
],
'BB' => [
22
],
'BC' => [
23
]
},
'C' => {
'CA' => [
24
],
'CB' => [
25
],
'CC' => [
26
],
'CD' => [
28
],
'CE' => [
29
],
'CF' => [
30
]
}
}
};
预期输出:
$VAR1 = {
'0' => {
'A' => {
'AA' => [
0
],
'AB' => [
1
],
'AC' => [
2
],
'AD' => [
3
]
},
'B' => {
'BA' => [
4
],
'BB' => [
5
],
'BC' => [
6
]
},
'C' => {
'CA' => [
7
],
'CB' => [
8
],
'CC' => [
9
]
}
},
'1' => {
'A' => {
'AA' => [
0
],
'AB' => [
1
],
'AC' => [
2
]
},
'B' => {
'BA' => [
3
],
'BB' => [
4
],
'BC' => [
5
],
'BD' => [
6
],
'BE' => [
7
]
},
'C' => {
'CA' => [
8
],
'CB' => [
9
],
'CC' => [
10
]
}
},
'2' => {
'A' => {
'AA' => [
0
],
'AB' => [
1
],
'AC' => [
2
]
},
'B' => {
'BA' => [
3
],
'BB' => [
4
],
'BC' => [
5
]
},
'C' => {
'CA' => [
6
],
'CB' => [
7
],
'CC' => [
8
],
'CD' => [
9
],
'CE' => [
10
],
'CF' => [
11
]
}
}
};
答案 0 :(得分:2)
您需要按社区迭代元素,为每个元素重置计数器。因此,我首先创建了没有计数器的结构,然后在设置值时以正确的顺序对其进行了迭代。
#!/usr/bin/perl
use strict;
use warnings;
use Tie::Autotie 'Tie::IxHash';
my @NAMES = qw(AA AB AC BA BB BC CA CB CC AA AB AC BA BB BC CA CB CC AA AB AC BA BB BC CA CB CC AD CD CE CF BD BE);
my @FAMILIES = qw(A A A B B B C C C A A A B B B C C C A A A B B B C C C A C C C B B);
my @COMMUNITIES = qw(0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 0 2 2 2 1 1);
tie my %community_family_name, 'Tie::IxHash' ;
undef $community_family_name{ $COMMUNITIES[$_] }{ $FAMILIES[$_]}{$NAMES[$_]}
for 0 .. $#NAMES;
for my $community (keys %community_family_name) {
my $i = 0;
for my $family (keys %{ $community_family_name{$community} }) {
for my $name (keys %{ $community_family_name{$community}{$family} }) {
$community_family_name{$community}{$family}{$name} = [$i++];
}
}
}
use Data::Dumper; print Dumper(\%community_family_name);
注意:您确定值必须在数组ref中吗?价值永远不会超过一个。
按照惯例,小写名称用于Perl中的可变变量。