在Perl中切片嵌套的哈希 [英] Slicing a nested hash in Perl
问题描述
说我有一个哈希,我可以将其索引为:
Say I have a hash that I can index as:
$hash{$document}{$word}
从我在网上阅读的内容(尽管我在 perlreftut , perldsc 或
From what I read online (although I could not find this on perlreftut, perldsc or perllol), I can slice a hash using a list if I use the @
prefix on my hash to indicate that I want the hash to return a list. However, if I try to slice my hash using a list @list
:
@%hash{$document}{@list}
我遇到几个"Scalar values ... better written"
错误.
如何在Perl中削减嵌套的哈希?
How can I slash a nested hash in Perl?
推荐答案
您的哈希的签名必须为@
,如下所示:
The sigill for your hash must be @
, like so:
@{$hash{$document}}{@list}
假定@list
包含%hash
的有效键,它将返回相应的值;如果键不存在,则返回undef
.
Assuming @list
contains valid keys for %hash
it will return the corresponding values, or undef
if the key does not exist.
这是基于哈希切片的一般规则:
This is based on the general rule of a hash slice:
%foo = ( a => 1, b => 2, c => 3 );
print @foo{'a','b'}; # prints 12
%bar = ( foo => \%foo ); # foo is now a reference in %bar
print @{ $bar{foo} }{'a','b'}; # prints 12, same data as before
这篇关于在Perl中切片嵌套的哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!