如何获取 Perl hash-of-hashes 中的二级密钥?

发布于 2024-08-13 18:24:49 字数 539 浏览 5 评论 0原文

我需要获取哈希中某个键的所有值。哈希看起来像这样:

$bean = {
     Key1 => {
               Key4 => 4,
               Key5 => 9,
               Key6 => 10,
             },
     Key2 => {
               Key7 => 5,
               Key8 => 9,
             },
};

例如,我只需要 Key4Key5Key6 的值。其余的不是兴趣点。我怎样才能得到这些值?

更新: 所以我没有 %bean 我只是将值添加到 $bean 中,如下所示:

 $bean->{'Key1'}->{'Key4'} = $value;

希望这有帮助。

I need to get all of the values for a certain key in a hash. The hash looks like this:

$bean = {
     Key1 => {
               Key4 => 4,
               Key5 => 9,
               Key6 => 10,
             },
     Key2 => {
               Key7 => 5,
               Key8 => 9,
             },
};

I just need the values to Key4, Key5 and Key6 for example. The rest is not the point of interest. How could I get the values?

Update:
So I don't have a %bean I just add the values to the $bean like this:

 $bean->{'Key1'}->{'Key4'} = $value;

hope this helps.

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

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

发布评论

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

评论(5

泅渡 2024-08-20 18:24:49
foreach my $key (keys %{$bean{Key1}})
{
  print $key . " ==> " . $bean{Key1}{$key} . "\n";
}

应该打印:

Key4 ==> 4
Key5 ==> 9
Key6 ==> 10
foreach my $key (keys %{$bean{Key1}})
{
  print $key . " ==> " . $bean{Key1}{$key} . "\n";
}

should print:

Key4 ==> 4
Key5 ==> 9
Key6 ==> 10
黄昏下泛黄的笔记 2024-08-20 18:24:49

如果 %bean 是哈希值的哈希值,则 $bean{Key1} 是哈希引用。要像操作简单哈希一样操作哈希引用,您需要取消引用它,如下所示:

%key1_hash = %{$bean{Key1}};

要访问哈希的哈希中的元素,请使用如下语法:

$element = $bean{Key1}{Key4};

因此,这是一个打印键和值的循环对于 $bean{Key1}

print $_, '=>', $bean{Key1}{$_}, "\n" for keys %{$bean{Key1}};

或者如果您只需要值,不需要键:

print $_, "\n" for values %{$bean{Key1}};

请参阅以下 Perl 文档以了解有关使用复杂数据结构的更多详细信息:perlreftut, perldscperllol

If %bean is a hash of hashes, $bean{Key1} is a hash reference. To operate on a hash reference as you would on a simple hash, you need to dereference it, like this:

%key1_hash = %{$bean{Key1}};

And to access elements within a hash of hashes, you use syntax like this:

$element = $bean{Key1}{Key4};

So, here's a loop that prints the keys and values for $bean{Key1}:

print $_, '=>', $bean{Key1}{$_}, "\n" for keys %{$bean{Key1}};

Or if you just want the values, and don't need the keys:

print $_, "\n" for values %{$bean{Key1}};

See the following Perl documentation for more details on working with complex data structures: perlreftut, perldsc, and perllol.

ペ泪落弦音 2024-08-20 18:24:49

还有一个解决方案:

for my $sh ( values %Bean ) {
    print "$_ => $sh->{$_}\n" for grep exists $sh->{$_}, qw(Key4 Key5 Key6);
}

Yet another solution:

for my $sh ( values %Bean ) {
    print "$_ => $sh->{$_}\n" for grep exists $sh->{$_}, qw(Key4 Key5 Key6);
}
甜中书 2024-08-20 18:24:49

请参阅 Perl 数据结构手册,了解使用 Perl 数据结构的大量示例。

See the Perl Data Structure Cookbook for lots of examples of, well, working with Perl data structures.

压抑⊿情绪 2024-08-20 18:24:49

做到这一点的一个好方法 - 假设您发布的内容是一个示例,而不是一个单一的案例 - 将是 递归地。因此,我们有一个函数,它可以在哈希中搜索我们指定的键,如果发现其中一个值是对另一个哈希的引用,则调用自身。

sub recurse_hash {
  # Arguments are a hash ref and a list of keys to find
  my($hash,@findkeys) = @_;

  # Loop over the keys in the hash
  foreach (sort keys %{$hash}) {

    # Get the value for the current key
    my $value = $hash->{$_};

    # See if the value is a hash reference
    if (ref($value) eq 'HASH') {
      # If it is call this function for that hash
      recurse_hash($value,@findkeys);
    }

    # Don't use an else in case a hash ref value matches our search pattern
    for my $key (@findkeys) {
      if ($key eq $_) {
        print "$_ = $value\n";
      }
    }
  }
}

# Search for Key4, Key5 and Key6 in %Bean
recurse_hash(\%Bean,"Key4","Key5","Key6");

给出这个输出:

Key4 = 4
Key5 = 9
Key6 = 10

A good way to do this - assuming what you're posting is an example, rather than a single one off case - would be recursively. So we have a function which searches a hash looking for keys we specify, calling itself if it finds one of the values to be a reference to another hash.

sub recurse_hash {
  # Arguments are a hash ref and a list of keys to find
  my($hash,@findkeys) = @_;

  # Loop over the keys in the hash
  foreach (sort keys %{$hash}) {

    # Get the value for the current key
    my $value = $hash->{$_};

    # See if the value is a hash reference
    if (ref($value) eq 'HASH') {
      # If it is call this function for that hash
      recurse_hash($value,@findkeys);
    }

    # Don't use an else in case a hash ref value matches our search pattern
    for my $key (@findkeys) {
      if ($key eq $_) {
        print "$_ = $value\n";
      }
    }
  }
}

# Search for Key4, Key5 and Key6 in %Bean
recurse_hash(\%Bean,"Key4","Key5","Key6");

Gives this output:

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