在 Perl 中填充哈希值的哈希值

发布于 2025-01-07 23:16:34 字数 304 浏览 0 评论 0原文

我想知道如何填充以下哈希结构:

my $hash = {
    'user' => [
        {
            'id' => '1',
            'name' => 'John'
        },
        {
            'id' => '2',
            'name' => 'Pat'
        }

    ]
};

我希望能够动态填充此哈希。我想循环数据库中的值并推送(添加)新值(id、名称)以填充哈希。

I want to know how to populate the following hash structure:

my $hash = {
    'user' => [
        {
            'id' => '1',
            'name' => 'John'
        },
        {
            'id' => '2',
            'name' => 'Pat'
        }

    ]
};

I want to be able to dynamically populate this hash. I want to loop around values from my database and push (add) new values (id, name) in order to populate the hash.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云柯 2025-01-14 23:16:34

为了填充散列的散列中的值,可以只使用:

my %hash;
....
$hash{$key}{$subkey} = $value;

但是,在循环它们时要小心!标量值 $hash{$key} 实际上是子哈希的标量引用,而不是子哈希对象本身。

In order to populate a value in a hash of hashes, one can just use :

my %hash;
....
$hash{$key}{$subkey} = $value;

Be careful when you loop through them, however! The scalar value $hash{$key} will actually be a scalar reference to the sub-hash, not the sub-hash object itself.

抚你发端 2025-01-14 23:16:34

要向该结构添加另一个元素:

# Get the element
my $hashRef = {
    'id' => '1',
    'name' => 'John'
};
# You didn't specify what you have as far as DB data
my $hashRef = someMethodReturningDatabaseData(); 

# Add to the arrayref:
my $hash = { user => [] }; # Initialize - only once
$hash->{user} ||= []; # As alternative, always make sure to have an arrayref,
                      # on every iteration
# Add to the array ref
push @{ $hash->{user} }, $hashRef;

根据您对另一个答案的评论,您可以执行

my $hash = { user => [] }; # Initialize - only once
foreach my $user (@users) {
    push @{ $hash->{user} }, { id => $user->[0], name => $user->[1] };
}

以下操作作为注释,您的数据结构看起来非常奇怪 - 不清楚为什么您需要仅带有一个硬编码键的外部哈希“用户”。

To add another element to that structure:

# Get the element
my $hashRef = {
    'id' => '1',
    'name' => 'John'
};
# You didn't specify what you have as far as DB data
my $hashRef = someMethodReturningDatabaseData(); 

# Add to the arrayref:
my $hash = { user => [] }; # Initialize - only once
$hash->{user} ||= []; # As alternative, always make sure to have an arrayref,
                      # on every iteration
# Add to the array ref
push @{ $hash->{user} }, $hashRef;

Based on your comment to another answer, you can do

my $hash = { user => [] }; # Initialize - only once
foreach my $user (@users) {
    push @{ $hash->{user} }, { id => $user->[0], name => $user->[1] };
}

As a note, your data structure looks very weird - it's not clear why you need the outer hash with just one hard-coded key "user".

南街九尾狐 2025-01-14 23:16:34
    my $user = {
        id   => $id,
        name => $name,
    };

    push @{ $hash->{user} }, $user;

Nit: $hash->{user} 应命名为 $hash->{users}

    my $user = {
        id   => $id,
        name => $name,
    };

    push @{ $hash->{user} }, $user;

Nit: $hash->{user} should be named $hash->{users}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文