如何将这些数组按顺序保存到此哈希数组中,而不是按字母顺序排列,只需按照数组进行保存即可。
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA -stackoverflow不允许我发布,因为没有足够的详细信息,所以我做到了-
#!/usr/bin/perl
use warnings;
use strict;
use Tie::IxHash;
use Data::Dumper;
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' ;
push @{ $COMMUNITY_FAMILY_NAME{ $COMMUNITY[$_] }{ $FAMILY[$_] } }, $NAME[$_] for 0 .. $#NAME;
print Dumper(\%COMMUNITY_FAMILY_NAME);
输出:
$VAR1 = {
'0' => {
'A' => [
'AA',
'AB',
'AC',
'AD'
],
'C' => [
'CA',
'CB',
'CC'
],
'B' => [
'BA',
'BB',
'BC'
]
},
'1' => {
'A' => [
'AA',
'AB',
'AC'
],
'C' => [
'CA',
'CB',
'CC'
],
'B' => [
'BA',
'BB',
'BC',
'BD',
'BE'
]
},
'2' => {
'A' => [
'AA',
'AB',
'AC'
],
'C' => [
'CA',
'CB',
'CC',
'CD',
'CE',
'CF'
],
'B' => [
'BA',
'BB',
'BC'
]
}
};
预期输出:
$VAR1 = {
'0' => {
'A' => [
'AA',
'AB',
'AC',
'AD'
],
'B' => [
'BA',
'BB',
'BC'
]
'C' => [
'CA',
'CB',
'CC'
],
},
'1' => {
'A' => [
'AA',
'AB',
'AC'
],
'B' => [
'BA',
'BB',
'BC',
'BD',
'BE'
'C' => [
'CA',
'CB',
'CC'
],
]
},
'2' => {
'A' => [
'AA',
'AB',
'AC'
],
'B' => [
'BA',
'BB',
'BC'
]
'C' => [
'CA',
'CB',
'CC',
'CD',
'CE',
'CF'
],
}
};
答案 0 :(得分:6)
您的哈希没有与Tie::IxHash
绑定,因此它们是普通/无序的。您可以在使用前通过每个子哈希tie
进行更改。
# push @{ $COMMUNITY_FAMILY_NAME{ $COMMUNITY[$_] }{ $FAMILY[$_] } }, $NAME[$_] for 0 .. $#NAME;
for (0 .. $#NAME) {
my $href = $COMMUNITY_FAMILY_NAME{ $COMMUNITY[$_] } ||= do {
tie my %hash, 'Tie::IxHash';
\%hash;
};
push @{ $href->{ $FAMILY[$_] } }, $NAME[$_];
}
答案 1 :(得分:4)
您遇到的问题是,绑定哈希仅影响一个级别-绑定哈希的每个值就是您存储在其中的值,通常是正常的perl值。这意味着,如果要使用多维绑定哈希,则必须绑定顶级变量以及存储在其中的所有值(在本例中为hashrefs)。
例如:
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
use Tie::IxHash;
tie my %hash, 'Tie::IxHash';
tie %{$hash{"b"}}, 'Tie::IxHash';
tie %{$hash{"c"}}, 'Tie::IxHash';
$hash{"b"}->{"b"} = [ qw/1 2 3/ ];
$hash{"b"}->{"a"} = [ qw/4 5 6/ ];
$hash{"c"}->{"d"} = [ qw/7 8 9/ ];
$hash{"c"}->{"c"} = [ qw/10 11 12/ ];
print Dumper(\%hash);