Perl 如何访问作为数组元素的散列,该数组是另一个散列的值?
我正在尝试创建一个以数组为值的哈希。
值(是一个数组)的第一个元素是标量。 该值的第二个元素(这是一个数组)是另一个散列。
我已将值放入此哈希的键和值中,如下所示:
${${$senseInformationHash{$sense}[1]}{$word}}++;
这里,
我的主哈希 -> senseInformationHash
我的值 ->是一个数组
所以, ${$senseInformationHash{$sense}[1]}
为我提供了对我的哈希的引用
,我输入了键和值,如下所示:
${${$senseInformationHash{$sense}[1]}{$word}}++;
我不确定这是否是正确的方法去做它。因为我被困住了,不知道如何打印这个复杂的东西。我想把它打印出来,以检查我是否做得正确。
任何帮助将非常感激。提前致谢!
I am trying to create a Hash that has as its value an array.
The first element of the value(which is an array) is a scalar.
The second element of the value(which is an array) is another hash.
I have put values in the key and value of this hash as follows :
${${$senseInformationHash{$sense}[1]}{$word}}++;
Here,
My main hash -> senseInformationHash
My Value -> Is an Array
So, ${$senseInformationHash{$sense}[1]}
gives me reference to my hash
and I put in key and value as follows :
${${$senseInformationHash{$sense}[1]}{$word}}++;
I am not sure if this is a correct way to do it. Since I am stuck and not sure how I can print this complex thing out. I want to print it out in order to check if I am doing it correctly.
Any help will be very much appreciated. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只要写
下来就可以了。
Perl 嫉妒 CamelCase,你知道,所以你应该使用正确的下划线。否则,它可能会吐口水、反抗,并且通常会行为不端。
Just write
and be done with it.
Perl gets jealous of CamelCase, you know, so you should use proper underscores. Otherwise it can spit and buck and generally misbehave.
哈希值从来都不是数组,它是数组引用。
要查看您是否做得正确,您可以转储整个结构:
这会让您:
注意
\1
:大概您希望该值为 1,而不是对标量 1 的引用。您得到后者是因为您的${ ... }++;
表示将大括号中的内容视为标量引用并增加引用的标量。${$senseInformationHash{$sense}[1]}{$word}++;
执行您想要的操作,$senseInformationHash{$sense}[1]{$word}+ 也是如此+。您可能会找到 http://perlmonks.org/?node=References+quick+reference 有助于了解原因。
A hash value is never an array, it is an array reference.
To see if you are doing it right, you can dump out the whole structure:
which gets you:
Note the
\1
: presumably you want the value to be 1, not a reference to the scalar 1. You are getting the latter because your${ ... }++;
says treat what's in the curly braces as a scalar reference and increment the scalar referred to.${$senseInformationHash{$sense}[1]}{$word}++;
does what you want, as does$senseInformationHash{$sense}[1]{$word}++
. You may find http://perlmonks.org/?node=References+quick+reference helpful in seeing why.谢谢 Axeman 和 TChrist。
我必须访问它的代码如下:
现在正在运行。非常感谢!
Thanks Axeman and TChrist.
The code I have to access it is as follows :
This is working now. Thanks much!