2
我正在嘗試使用sql查詢的結果創建數組的哈希值: 例如,將數組列名作爲鍵的Perl哈希值
列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(keys(%$ row)){ push @ {$ hash_ref {$ col_name}},$ row - > {$ {col_name}}; } 謝謝你的幫助! – user2052673
的確,哎呀!固定。 ('$ row - > {$ col_name}''就足夠了) – ikegami