2

我正在尝试使用 sql 查询的结果创建数组哈希:例如。

第 1 列:

1                                         
2
3
4

第 2 列:

A 
B 
C 
D

期望的结果:

my %by_col = (
    'Column1'=>['1','2','3','4'],
    'Column2'=>['A','B','C','D'],
);

我可以使用以下方法将结果作为哈希数组获得:

while ($hash_ref = $sth->fetchrow_hashref()) {
    push @$hash_array_ref, { %$hash_ref };
}

但是反过来想不通。

谢谢!

4

1 回答 1

1
while (my $row = $sth->fetchrow_hashref()) {
   for my $col_name (keys(%$row)) {
      push @{ $by_col{$col_name} }, $row->{$col_name};
   }
}
于 2013-02-07T23:41:53.090 回答