为什么我不能在 Perl 中将项目推送到数组的散列中?

Why can't I push items into a hash of arrays, in Perl?

这里我想要的是@{$allHash{$key1}}是["item1", "item2"].

推送不起作用,所有打印语句都是试图找到推送项目的位置。 在推送中使用 -> 符号更糟糕,它使 $temp[0][0] 和第 24 行显示一个数组而不是项目。

    #use strict;   # I've turned these off here to make the code easier to read
    #use warnings;

    my %allHash = ();
    my $key1 = "key1";
    my $item1 = "item1";
    my $item2 = "item2";
    #if (!exists($allHash{key1})) {$allHash{key1}=();}; # makes no difference, the array autovivefies anyway

    push (@{$allHash{$key1}},  $item1);         # push 1st item
    print"\n\nat11: pushed $key1, $item1";
    my @temp = $allHash{$key1};
    print"\nat13:temp=@temp, length=",0+@temp, ", temp[0]=$temp[0], temp[0][0]=$temp[0][0]";
    print"\nat14: allHash{$key1}[0]= $allHash{$key1}[0]";
    print"\nat15: allHash{$key1}[1]= $allHash{$key1}[1]";
    print"\nat16: allHash{$key1}[0][0]= $allHash{$key1}[0][0]";
    print"\nat17: allHash{$key1}[1][0]= $allHash{$key1}[1][0]";
    print"\nat18: allHash{$key1}[0][1]= $allHash{$key1}[0][1]\n";
    print"\n----------------";
    
    push (@{$allHash{$key1}},  $item2);         # push 2d item
    print"\n\nat21: pushed $key1, $item2";
    @temp = @{allHash{$key1}};
    print"\nat23:temp=@temp, length=",0+@temp, ", temp[0]=$temp[0], temp[0][0] =$temp[0][0]";
    print"\nat24: allHash{$key1}[0]= $allHash{$key1}[0]";
    print"\nat25: allHash{$key1}[1]= $allHash{$key1}[1], allHash{$key1}[1][0] =$allHash{$key1}[1][0]";
    print"\nat26: allHash{$key1}[0][0]= $allHash{$key1}[0][0]";
    print"\nat27: allHash{$key1}[1][0]= $allHash{$key1}[1][0]";
    print"\nat28: allHash{$key1}[0][1]= $allHash{$key1}[0][1]\n";

上述程序的输出是:

at11: pushed key1, item1
at13:temp=, length=1, temp[0]=ARRAY(0x331eb8), temp[0][0]=item1
at14: allHash{key1}[0]=item1
at15: allHash{key1}[1]=
at16: allHash{key1}[0][0]=
at17: allHash{key1}[1][0]=
at18: allHash{key1}[0][1]=

----------------

at21: pushed key1, item2
at23:temp=ARRAY(0x331ee8), length=1, temp[0]=ARRAY(0x331ee8), temp[0][0]=item1
at24: allHash{key1}[0]=item1
at25: allHash{key1}[1]= ARRAY(0x332020), allHash{key1}[1][0]=
at26: allHash{key1}[0][0]=
at27: allHash{key1}[1][0]=
at28: allHash{key1}[0][1]=

奇怪的是,我的另一个程序中的这段几乎相同的代码运行得很好。

%hedgeHash = ();    # collect the members of each hedge as an array, using stub as key
for (my $i=0; $i<@options; $i++)
{   $Hstub = $options[$i][$iStub];
    push @{$hedgeHash{$Hstub}}, $i; # hedgehash should contain array of members of the hedge.
}

更奇怪的是,如果我从 push 语句中删除括号,我不再得到 'item1' 作为 @temp 和第 14 行和第 24 行的输出,而是得到另一个数组! WTF??

请参阅下面的示例代码,演示数组散列的使用。

确实OP的编码风格让代码阅读有些困难。

use strict;
use warnings;
use feature 'say';

use Data::Dumper;

my %allHash;
my $key1 = 'key1';
my $item1 = 'item1';
my $item2 = 'item2';
my $item3 = 'item3';
my $item4 = 'item4';
my $item5 = 'item5';

push @{$allHash{$key1}}, $item1;
push @{$allHash{$key1}}, $item2;

$allHash{$key1}[2] = $item3;
$allHash{$key1}[3] = [$item4,$item5];

say Dumper(\%allHash);

输出

$VAR1 = {
          'key1' => [
                      'item1',
                      'item2',
                      'item3',
                      [
                        'item4',
                        'item5'
                      ]
                    ]
        };