如何在perl中迭代多级哈希
试试这个:如何在perl中迭代多级哈希,perl,Perl,试试这个: id score 0 4 1 5 2 6 3 7 4 8 5 9 打印“id\t核心”; 对于($i=0;$i) 或者这里有一种不同的方法,我认为更容易: id score 0 4 1 5 2 6 3 7 4 8 my$app=“info”; 我的%记录; 对于(我的$i=0;$i$i,score=>$i+4}; } printf“id\t存储\n”; 对于我的$应用程序(键%records){ my@apprecord
id score
0 4
1 5
2 6
3 7
4 8
5 9
打印“id\t核心”;
对于($i=0;$i)
或者这里有一种不同的方法,我认为更容易:
id score
0 4
1 5
2 6
3 7
4 8
my$app=“info”;
我的%记录;
对于(我的$i=0;$i<5;$i++)
{
#$records{$app}是哈希列表,例如。
#$records{info}[0]{id}
推送{$records{$app},{id=>$i,score=>$i+4};
}
printf“id\t存储\n”;
对于我的$应用程序(键%records){
my@apprecords=@{$records{$app};
对于我的$apprecordref(@apprecords){
我的%apprecord=%$apprecordref;
printf“%d\t%d\n”,$apprecord{“id”},$apprecord{“score”};
}
}
print "id\tscore";
for($i=0; $i<5; $i++) {
print "\n$records{$app}{id}[$i]\t$records{$app}{score}[$i]";
}
printf "id\tscore\n";
for my $app (keys %records) {
my $apprecordref = $records{$app};
my %apprecord = %$apprecordref;
my $idlen = scalar(@{$apprecord{"id"}});
for ($i = 0; $i < $idlen; $i++) {
printf "%d\t%d\n", $apprecord{"id"}[$i], $apprecord{"score"}[$i];
}
}
id score
0 4
1 5
2 6
3 7
4 8
my $app = "info";
my %records;
for (my $i = 0; $i < 5; $i++)
{
# $records{$app} is a list of hashes, e.g.
# $records{info}[0]{id}
push @{$records{$app}}, {id=>$i, score=>$i+4};
}
printf "id\tscore\n";
for my $app (keys %records) {
my @apprecords = @{$records{$app}};
for my $apprecordref (@apprecords) {
my %apprecord = %$apprecordref;
printf "%d\t%d\n", $apprecord{"id"}, $apprecord{"score"};
}
}