Perl哈希错误的动态哈希

问题描述 投票:1回答:1

我循环查询并返回结果。我正在尝试将哈希添加到另一个哈希。但遇到麻烦。

my %users_data;
while($sth->fetch)
{
    $mygroup =>
    {
        'fname' => $fname,
        'lname' => $lname,
        'address' =>
        {
           'street' => $street,
           'city' => $city,
        },
        'id' => $uid,
     },
}

如何将$mygroup哈希添加到%users_data哈希?

长手会。

my %users_data = (
 'salesmanager' =>
 {
    'fname' => 'mike',
    'lname' => 'john',
    'address' =>
    {
       'street' => '123 street',
       'city' => 'Brooklyn',
    },
    'id' => 12,
 },
 'garagemanager' =>
 {
    'fname' => 'Mark',
    'lname' => 'Jones',
    'address' =>
    {
       'street' => '355 street',
       'city' => 'Brooklyn',
    },
    'id' => 13,
 },
)
perl hash hash-of-hashes
1个回答
4
投票

只需访问目标键并分配其新值:

my %users_data;
while($sth->fetch)
{
    $users_data{$mygroup} = {
        'fname' => $fname,
        'lname' => $lname,
        'address' =>
        {
           'street' => $street,
           'city' => $city,
        },
        'id' => $uid,
     };
}
© www.soinside.com 2019 - 2024. All rights reserved.