Reputation: 23
I am trying to create a hash of arrays using the result of a sql query: Eg.
Column1:
1
2
3
4
Column2:
A
B
C
D
Desired result:
my %by_col = (
'Column1'=>['1','2','3','4'],
'Column2'=>['A','B','C','D'],
);
I am able to get the result as array of hashes using:
while ($hash_ref = $sth->fetchrow_hashref()) {
push @$hash_array_ref, { %$hash_ref };
}
But can't figure out the other way round.
Thanks!
Upvotes: 2
Views: 382
Reputation: 385829
while (my $row = $sth->fetchrow_hashref()) {
for my $col_name (keys(%$row)) {
push @{ $by_col{$col_name} }, $row->{$col_name};
}
}
Upvotes: 1