perl-将一个数据结构转换为另一个数据结构
我正在尝试使用Perl将一个数据结构转换为另一种数据结构。
my $agent_details = $dbh->selectall_arrayref(
"SELECT agent_id, year, type FROM agents ORDER BY agent_id",
{ Slice => {} }
);
我最终以数据结构的方式:
$VAR1 = [
{
'agent_id' => '1',
'year' => '2000',
'type' => '14',
},
{
'agent_id' => '2',
'year' => '2001',
'type' => '14',
},
{
'agent_id' => '3',
'year' => '2002',
'type' => '14',
},
{
'agent_id' => '4',
'year' => '2000',
'type' => '14',
},
{
'agent_id' => '5',
'year' => '2001',
'type' => '14',
},
{
'agent_id' => '6',
'year' => '2002',
'type' => '14',
},
]
我想做的就是变成阵列的哈希:
$VAR2 = {
'2000' => [1, 4],
'2001' => [2, 5],
'2002' => [3, 6],
}
我能够用一些丑陋的循环来完成此操作,但是我觉得有一种更容易 /更好的清洁方法可以做到这一点。
I'm trying to convert one data structure into another, using Perl.
my $agent_details = $dbh->selectall_arrayref(
"SELECT agent_id, year, type FROM agents ORDER BY agent_id",
{ Slice => {} }
);
I end up with the data structure:
$VAR1 = [
{
'agent_id' => '1',
'year' => '2000',
'type' => '14',
},
{
'agent_id' => '2',
'year' => '2001',
'type' => '14',
},
{
'agent_id' => '3',
'year' => '2002',
'type' => '14',
},
{
'agent_id' => '4',
'year' => '2000',
'type' => '14',
},
{
'agent_id' => '5',
'year' => '2001',
'type' => '14',
},
{
'agent_id' => '6',
'year' => '2002',
'type' => '14',
},
]
What I would like to do is transform into a hash of arrays:
$VAR2 = {
'2000' => [1, 4],
'2001' => [2, 5],
'2002' => [3, 6],
}
I was able to accomplish this with some ugly loops, but I feel like there is an easier / better cleaner way to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)