将数组添加到已存储数组的哈希条目

Add an array to a hash entry that is already storing an array

我需要计算一系列要转换为数组散列的字符串。考虑到,在这种情况下,我想将一个数组添加到哈希中的一个条目,它已经存储了一个数组。我需要获取以下哈希值:

ConfigurationHash{'Editor'} = (John, Mary, Jane, Peter)

我已将我的代码简化为这个示例:

use strict;
use warnings;

my %ConfigurationHash;

my $String1 = "Editor=John,Mary";
my $String2 = "Editor=Jane,Peter";

my @Line1 = split ("=", $String1);
my @Line2 = split ("=", $String2);

my $Variable1 = @Line1[0];
my $Value1    = @Line1[1];

my $Variable2 = @Line2[0];
my $Value2    = @Line2[1];

my @Values1Array = split(",", $Value1);
my @Values2Array = split(",", $Value2);

if ( ! exists $ConfigurationHash{$Variable1} ) {
    $ConfigurationHash{$Variable1} = @Values1Array;
}
else {
    push (@ConfigurationHash{$Variable1}, @Values1Array);
}

产生以下错误:

Experimental push on scalar is now forbidden at ./test.pl line 25, near "@Values1Array)" Execution of ./test.pl aborted due to compilation errors.

我知道问题出在references/dereferences,但我对 perl 的了解太基础了,我无法自己弄清楚如何到达那里。

谁能告诉我怎么做?如果您能告诉我如何在创建散列后迭代散列中的数组值,我将不胜感激。

这条线并没有像你想象的那样。

$ConfigurationHash{$Variable1} = @Values1Array;

如果您打印出 $ConfigurationHash{$Variable1} 包含的内容,您会发现它只包含 @Values1Array 的大小。

您应该可以使用 push,但对您编写的内容稍作修改。

push @{$ConfigurationHash{$Variable1}}, @Values1Array;

我还删除了括号,因为您不需要它们。

至于遍历数组,它与遍历常规数组没有什么不同。您之前可能在迭代它时遇到问题,因为您没有数组

foreach my $whatever (@{$ConfigurationHash{$Variable1}})
  {
  # Code
  }

不清楚为什么您的代码中有 $String2 及其派生词,因为它们从未被使用过。此代码处理两个字符串

您只需要 push 值列表到与散列中的 $Variable1(可怕的标识符选择)对应的数组。通过 取消引用 数组元素

来完成此操作
use strict;
use warnings;

my %config;

my $s1 = 'Editor=John,Mary';
my $s2 = 'Editor=Jane,Peter';

for ( $s1, $s2 ) {
    my ($key, @values) = split /[=,]/;
    push @{ $config{$key} }, @values;
}

use Data::Dumper;
print Dumper \%config;

产出

$VAR1 = {
          'Editor' => [
                        'John',
                        'Mary',
                        'Jane',
                        'Peter'
                      ]
        };

感谢所有发布答案的人。 @Borodin,你是对的,我错过了使用 $String2 及其派生词的第二个块,但我认为很明显它在最后并且类似于我原始代码中的 if-else 块。

谢谢你,@chris-turner,给了我如何正确使用 push 的提示,并指出了 $ConfigurationHash{$Variable1} = @Values1Array;

中的错误

通过所有这些贡献,我发现我期望的正确代码是:

use strict;
use warnings;

my %ConfigurationHash;

my $String1 = "Editor=John,Mary";
my $String2 = "Editor=Jane,Peter";

my @Line1 = split ("=", $String1);
my @Line2 = split ("=", $String2);

my $Variable1 = $Line1[0];
my $Value1    = $Line1[1];

my $Variable2 = $Line2[0];
my $Value2    = $Line2[1];

my @Values1Array = split(",", $Value1);
my @Values2Array = split(",", $Value2);

if ( ! exists $ConfigurationHash{$Variable1} ) {
    $ConfigurationHash{$Variable1} = \@Values1Array;
}
else {
    #push (@ConfigurationHash{$Variable1}, @Values1Array);
    push @{$ConfigurationHash{$Variable1}}, @Values1Array;
}

if ( ! exists $ConfigurationHash{$Variable2} ) {
    $ConfigurationHash{$Variable2} = \@Values2Array;
}
else {
    #push (@ConfigurationHash{$Variable2}, @Values2Array);
    push @{$ConfigurationHash{$Variable2}}, @Values2Array;
}

use Data::Dumper;
print Dumper \%ConfigurationHash;

输出如下:

$VAR1 = {
          'Editor' => [
                        'John',
                        'Mary',
                        'Jane',
                        'Peter'
                      ]
        };