简体   繁体   English

Perl如何访问作为另一个哈希值的数组元素的哈希?

[英]Perl How to access a hash that is the element of an array that is the value of another hash?

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 我的主要哈希-> senseInformationHash

My Value -> Is an Array 我的价值->是一个数组

So, ${$senseInformationHash{$sense}[1]} gives me reference to my hash 因此, ${$senseInformationHash{$sense}[1]}给了我对我的哈希的引用

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! 提前致谢!

Just write 写吧

$sense_information_hash{$sense}[1]{$word}++;

and be done with it. 并完成它。

Perl gets jealous of CamelCase, you know, so you should use proper underscores. 您知道Perl嫉妒CamelCase,因此应该使用适当的下划线。 Otherwise it can spit and buck and generally misbehave. 否则,它可能会吐口水,而且通常行为不端。

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: 要查看您是否做对了,可以转储整个结构:

my %senseInformationHash;
my $sense = 'abc';
my $word = '123';
${${$senseInformationHash{$sense}[1]}{$word}}++;
use Data::Dumper;
print Dumper( \%senseInformationHash );

which gets you: 这使您:

$VAR1 = {
      'abc' => [
                 undef,
                 {
                   '123' => \1
                 }
               ]
    };

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 ${ ... }++; 注意\\1 :大概您希望该值为1,而不是对标量1的引用。之所以得到后者,是因为您的${ ... }++; 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}++ . $senseInformationHash{$sense}[1]{$word}++您的需求, $senseInformationHash{$sense}[1]{$word}++ You may find http://perlmonks.org/?node=References+quick+reference helpful in seeing why. 您可能会发现http://perlmonks.org/?node=References+quick+reference有助于了解原因。

Thanks Axeman and TChrist. 感谢Axeman和TChrist。

The code I have to access it is as follows : 我必须访问它的代码如下:

    foreach my $outerKey (keys(%sense_information_hash))
{
  print "\nKey => $outerKey\n";
  print "  Count(sense) => $sense_information_hash{$outerKey}[0]\n";

        foreach(keys (%{$sense_information_hash{$outerKey}[1]}) )
    {
        print " Word wt sense => $_\n";
        print " Count  => $sense_information_hash{$outerKey}[1]{$_}\n"; 
    }
}

This is working now. 现在正在工作。 Thanks much! 非常感谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM