【问题标题】:Perl - Iterating through an array of hashes issuePerl - 遍历哈希数组问题
【发布时间】:2015-02-13 18:04:16
【问题描述】:

Perl 的新手,所以我怀疑有一个简单的解决方案,但尽管进行了大量的谷歌搜索,但我看不到它。

my @special_things = get_special_things(\@allThings);

sub get_special_things {
    my $things = shift;
    my @specialThings;

    foreach my $thing (@$things) {
        if ($thing{special} == 1) {
            push(@specialThings, $things);
        } 
    }
    return @specialThings;
}

传入的 allThings 数组是一个哈希数组。我在 foreach 行上收到错误消息,告诉我“全局符号“%thing”需要显式包名”。

我知道这与引用哈希值或键有关,但我现在不知所措。非常感谢任何帮助。

【问题讨论】:

  • $thing{special} 表示哈希%thing的键为special的元素

标签: arrays perl hash


【解决方案1】:

您有一个 hashrefs 数组,而不是哈希数组。使用 hashrefs 时需要使用$thing->{special}

【讨论】:

    猜你喜欢
    • 2012-08-22
    • 2013-11-10
    • 2013-01-16
    • 2019-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-30
    • 2016-08-20
    相关资源
    最近更新 更多