Perl哈希错误的动态哈希

时间:2019-02-28 16:29:52

标签: perl hash hash-of-hashes

我正在遍历查询并返回结果。我试图将哈希添加到另一个哈希。但是遇到麻烦了。

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,
 },
)

1 个答案:

答案 0 :(得分:4)

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

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