Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 数组列名作为键的Perl哈希_Arrays_Perl_Hash - Fatal编程技术网

Arrays 数组列名作为键的Perl哈希

Arrays 数组列名作为键的Perl哈希,arrays,perl,hash,Arrays,Perl,Hash,我正在尝试使用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->fetch

我正在尝试使用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 };
}
但我想不出另一条路


谢谢

您的代码无疑为我指明了正确的方向,但是,所有值都显示为“undef”。我对它做了一点修改,现在看起来效果不错。foreach my$col_name(key(%$row)){push@{$hash_ref{$col_name}},$row->{${col_name};}感谢您的帮助!真的,哎呀!固定的。(
$row->{$col_name}
就足够了)
while (my $row = $sth->fetchrow_hashref()) {
   for my $col_name (keys(%$row)) {
      push @{ $by_col{$col_name} }, $row->{$col_name};
   }
}