How to iterate over multilevel hash in perl -
my $app = "info"; %records; for($i = 0; $i<5; $i++) { push@{$records{$app}{"id"}},$i; push@{$records{$app}{"score"}}, $i+4; }
so there 5 ids [0,1,2,3,4,5] , 5 scores .my question how iterate on each id , corresponding score ...please me ..basically want print result in way
id score 0 4 1 5 2 6 3 7 4 8 5 9
printf "id\tscore\n"; $app (keys %records) { $apprecordref = $records{$app}; %apprecord = %$apprecordref; $idlen = scalar(@{$apprecord{"id"}}); ($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 different way of doing think bit easier:
my $app = "info"; %records; (my $i = 0; $i < 5; $i++) { # $records{$app} list of hashes, e.g. # $records{info}[0]{id} push @{$records{$app}}, {id=>$i, score=>$i+4}; } printf "id\tscore\n"; $app (keys %records) { @apprecords = @{$records{$app}}; $apprecordref (@apprecords) { %apprecord = %$apprecordref; printf "%d\t%d\n", $apprecord{"id"}, $apprecord{"score"}; } }
Comments
Post a Comment