Perl 如何访问作为数组元素的散列,该数组是另一个散列的值?

发布于 2024-10-02 05:04:07 字数 522 浏览 1 评论 0原文

我正在尝试创建一个以数组为值的哈希。

值(是一个数组)的第一个元素是标量。 该值的第二个元素(这是一个数组)是另一个散列。

我已将值放入此哈希的键和值中,如下所示:

${${$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

近箐 2024-10-09 05:04:08

只要写

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

下来就可以了。

Perl 嫉妒 CamelCase,你知道,所以你应该使用正确的下划线。否则,它可能会吐口水、反抗,并且通常会行为不端。

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. Otherwise it can spit and buck and generally misbehave.

享受孤独 2024-10-09 05:04:08

哈希值从来都不是数组,它是数组引用。

要查看您是否做得正确,您可以转储整个结构:

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

这会让您:

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

注意 \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:

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 ${ ... }++; 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.

下壹個目標 2024-10-09 05:04:08

谢谢 Axeman 和 TChrist。

我必须访问它的代码如下:

    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"; 
    }
}

现在正在运行。非常感谢!

Thanks Axeman and 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文