考虑示例代码:
$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',
}
}
};我将其作为引用返回,返回\%hash
我该如何取消引用呢?
发布于 2013-05-15 15:11:57
%$hash。有关详细信息,请参阅http://perldoc.perl.org/perlreftut.html。
如果您的散列是由函数调用返回的,则可以执行以下任一操作:
my $hash_ref = function_call();
for my $key (keys %$hashref) { ... # etc: use %$hashref to dereference或者:
my %hash = %{ function_call() }; # dereference immediately要访问散列中的值,可以使用->运算符。
$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发布于 2013-05-15 15:41:57
试试下面这样的东西,可能会对你有帮助:
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";
}
}
}此外,如果您想访问特定的密钥,您可以这样做
my $test = $VAR1->{es}->{new}->{id};
https://stackoverflow.com/questions/16558872
复制相似问题