Frank
Frank

Reputation: 1130

Perl - Iterating through an array of hashes issue

Newcomer to Perl so I suspect there is a simple solution to this but I cannot see it despite extensive Googling.

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

The array of allThings being passed in is an array of hashes. I am getting an error on the foreach line telling me that 'Global symbol "%thing" requires explicit package name'.

I know this has something to do with referencing the hash value or key but I am at a loss at the minute. Any help is much appreciated.

Upvotes: 1

Views: 56

Answers (1)

Dre
Dre

Reputation: 4329

You have an array of hashrefs, not of hashes. You need to use $thing->{special} when when working with hashrefs.

Upvotes: 5

Related Questions