【问题标题】:Add an array to a hash entry that is already storing an array将数组添加到已存储数组的哈希条目
【发布时间】:2018-11-28 09:39:18
【问题描述】:

我需要评估一系列要转换为数组哈希的字符串。考虑到,在这种情况下,我想将一个数组添加到已经存储数组的哈希条目中。我需要得到以下哈希:

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

这会产生以下错误:

现在禁止在 ./test.pl 第 25 行“@Values1Array)”附近对标量进行实验性推送 ./test.pl 的执行由于编译错误而中止。

我知道问题在于引用/取消引用,但我对 perl 的了解太基础了,以至于我无法自己弄清楚如何到达那里。

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

【问题讨论】:

  • 您似乎误解了一些数据结构基础知识。我建议你阅读perldoc.perl.org/perldsc.html
  • “哈希中的一个条目,它已经存储了一个数组” 我不明白你的这部分问题。您的 %ConfigurationHash 最初为空。

标签: arrays perl hashtable


【解决方案1】:

不清楚为什么您的代码中有$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'
                      ]
        };

【讨论】:

    【解决方案2】:

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

    $ConfigurationHash{$Variable1} = @Values1Array;
    

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

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

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

    我也删除了括号,因为你不需要它们。

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

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

    【讨论】:

    • "你会发现它只包含@Values1Array的最后一个元素" 这是完全错误的。数组与列表不同。数组在标量上下文中返回它们的 length,在这种情况下,哈希元素将设置为 2。
    • @Borodin 啊,感谢您指出这一点 - 没有发现这一点,因为我正在测试的数组中有数字
    【解决方案3】:

    感谢所有发布答案的人。 @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'
                          ]
            };
    

    【讨论】:

      猜你喜欢
      • 2012-09-03
      • 2011-07-26
      • 1970-01-01
      • 2020-06-30
      • 1970-01-01
      • 2020-06-08
      • 2012-01-07
      • 2020-07-24
      • 2017-09-20
      相关资源
      最近更新 更多