[英]Perl: dereferencing an hash of hash of hashes
consider the sample code: 考虑示例代码:
$VAR1 = {
'en' => {
'new' => {
'style' => 'defaultCaption',
'tts:fontStyle' => 'bold',
'id' => 'new'
},
'defaultCaption' => {
'tts:textAlign' => 'left',
'tts:fontWeight' => 'normal',
'tts:color' => 'white',
}
},
'es' => {
'defaultSpeaker' => {
'tts:textAlign' => 'left',
'tts:fontWeight' => 'normal',
},
'new' => {
'style' => 'defaultCaption',
'tts:fontStyle' => 'bold',
'id' => 'new'
},
'defaultCaption' => {
'tts:textAlign' => 'left',
'tts:fontWeight' => 'normal',
}
}
};
i return it as reference, return \\%hash 我将它作为参考返回,返回\\%hash
how do i dereference this? 我怎么解除这个?
%$hash
. %$hash
。 See http://perldoc.perl.org/perlreftut.html for more information. 有关更多信息,请参见http://perldoc.perl.org/perlreftut.html 。
If your hash is returned by a function call, you can do either: 如果函数调用返回了哈希值,则可以执行以下任一操作:
my $hash_ref = function_call();
for my $key (keys %$hashref) { ... # etc: use %$hashref to dereference
Or: 要么:
my %hash = %{ function_call() }; # dereference immediately
To access values within your hash, you can use the ->
operator. 要访问哈希值中的值,可以使用
->
运算符。
$hash->{en}; # returns hashref { new => { ... }. defaultCaption => { ... } }
$hash->{en}->{new}; # returns hashref { style => '...', ... }
$hash->{en}{new}; # shorthand for above
%{ $hash->{en}{new} }; # dereference
$hash->{en}{new}{style}; # returns 'defaultCaption' as string
try something like below, might be helpful for you: 尝试类似下面的内容,可能对您有所帮助:
my %hash = %{ $VAR1};
foreach my $level1 ( keys %hash) {
my %hoh = %{$hash{$level1}};
print"$level1\n";
foreach my $level2 (keys %hoh ) {
my %hohoh = %{$hoh{$level2}};
print"$level2\n";
foreach my $level3 (keys %hohoh ) {
print"$level3, $hohoh{$level3}\n";
}
}
}
Moreover, if you want to access the specific key, you can do it like 此外,如果您想访问特定密钥,您可以这样做
my $test = $VAR1->{es}->{new}->{id};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.