繁体   English   中英

在 Perl 中,如何从散列的 hash 访问元素

[英]In Perl, how do you access an element from a hash of hashes

在这个例子中,我想从$ref中读取字母“d”:

$ref={a,b,c,{d,e}}
# Start using these!
use strict;
use warnings;

# A more standard way of writing your example.
my $ref = { a => "b", c => { d => "e", f => "g" } };

# How to access elements within the structure.
my $inner = $ref->{c};
print $_, "\n" for
    $inner->{d},   # e
    keys %$inner,  # d f
    $ref->{c}{d},  # e    (directly, without using intermediate variable).
;

有关详细信息,请参阅Perl 数据结构说明书

print keys %{$ref->{c}}; 将适用于那个特定的(可怕的)例子。 它可能会也可能不会解决您的问题,因为我们不知道问题到底是什么。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM