Reputation: 105
my $app = "info";
my %records;
for($i = 0; $i<5; $i++)
{
push@{$records{$app}{"id"}},$i;
push@{$records{$app}{"score"}}, $i+4;
}
so there are 5 ids [0,1,2,3,4,5] and 5 scores .my question is how to iterate over each id and corresponding score ...please help me ..basically i want to print the result in this way
id score
0 4
1 5
2 6
3 7
4 8
5 9
Upvotes: 2
Views: 1106
Reputation: 25656
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
Or here is a different way of doing it that I think is a bit easier:
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"};
}
}
Upvotes: 0
Reputation: 6299
Try this:
print "id\tscore";
for($i=0; $i<5; $i++) {
print "\n$records{$app}{id}[$i]\t$records{$app}{score}[$i]";
}
Upvotes: 1