$hash = { 'Man' => 'Bill',
'Woman' => 'Mary,
'Dog' => 'Ben'
};
Perl的“匿名哈希”到底是做什么的?
最佳答案
它是对可以存储在标量变量中的哈希的引用。除了大括号{...}
创建对哈希的引用之外,它与常规哈希完全相同。
请注意这些示例中不同括号的用法:
%hash = ( foo => "bar" ); # regular hash
$hash = { foo => "bar" }; # reference to anonymous (unnamed) hash
$href = \%hash; # reference to named hash %hash
例如,如果您想将散列作为参数传递给子例程,这将很有用:
foo(\%hash, $arg1, $arg2);
sub foo {
my ($hash, @args) = @_;
...
}
这是创建多层哈希的一种方法:
my %hash = ( foo => { bar => "baz" } ); # $hash{foo}{bar} is now "baz"
关于perl - Perl中的匿名哈希是什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14175585/