[英]Different declaration of hash in Perl
I know that: 我知道:
my %hash = {};
gets an anonymous hash, How about this: 得到一个匿名哈希,如何处理:
my %hash = %{some values}
what is the difference of that above with this? 这与上面有什么区别?
my %hash = (some hash values);
No. 没有。
my %hash = {};
generates a warning (you turned them on, right?): 生成警告(您将其打开,对吗?):
Reference found where even-sized list expected at -e line 1.
Reference is always scalar. 参考始终是标量。 The correct way is
正确的方法是
my $hash_ref = {};
To dereference a reference, you can use the following syntax: 要取消引用的引用,可以使用以下语法:
my %hash = %$hash_ref;
my %also_hash = %{$hash_ref}; # Same as above.
$hash{key} eq $hash_ref->{key} or die; # Should survive.
Moreover, 此外,
%{ some values }
generates a syntax error: 产生语法错误:
perl -we 'my $h = %{1, 2, 3, 4}'
syntax error at -e line 1, near "%{"
The difference is how you express the content of the hash. 区别在于您如何表达哈希的内容。 With the array notation below, for example;
例如,下面的数组符号; you do it like this:
你这样做:
my %hash = ( 'key 1' => 'value 1', 'key 2' => 'value 2');
The %{ } is a cast operator. %{}是强制转换运算符。 It is used typically when you have a reference to something that is not obviously a hash.
通常,当您引用的内容显然不是哈希时,通常使用它。 Typically a reference to a has:
通常,对a的引用具有:
Example: 例:
my $hashref;
$hashref->{'key 1'}='value 1';
$hashref->{'key 2'}='value 2';
my %hash = %{$hashref};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.