Perl, get all hash values
Posted
by Mike
on Stack Overflow
See other posts from Stack Overflow
or by Mike
Published on 2010-06-15T20:16:28Z
Indexed on
2010/06/15
20:22 UTC
Read the original article
Hit count: 165
Let's say in Perl I have a list of hash references, and each is required to contain a certain field, let's say foo
. I want to create a list that contains all the mappings of foo
. If there is a hash that does not contain foo
the process should fail.
@hash_list = (
{foo=>1},
{foo=>2}
);
my @list = ();
foreach my $item (@hash_list) {
push(@list,$item->{foo});
}
#list should be (1,2);
Is there a more concise way of doing this in Perl?
© Stack Overflow or respective owner