Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to extract a value from all the hashes in a list of hashes?

Reply
Thread Tools

How to extract a value from all the hashes in a list of hashes?

 
 
mrstevegross
Guest
Posts: n/a
 
      11-11-2008
I have a list of hashes that looks like this:

my @list = (
{ Name => 'Foo', X => Y },
{ Name => 'Bar', X => Y },
{ Name => 'Baz', X => Y },
);

I would like to iterate across the list-of-hashes and extract just the
values of the 'Name' key as a list. I had hoped that the following
code would do the trick:

my @names = grep { $_->{Name} } @list;

But it doesn't, because the grep simply matches the hash reference but
doesn't return the value of the named attribute. Is there a quick way
to make this work, other than iterating across the list with a
foreach() statement?

Thanks,
--Steve
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      11-11-2008
mrstevegross <(E-Mail Removed)> wrote:
>I have a list of hashes that looks like this:
>
>my @list = (
> { Name => 'Foo', X => Y },
> { Name => 'Bar', X => Y },
> { Name => 'Baz', X => Y },
>);
>
>I would like to iterate across the list-of-hashes and extract just the
>values of the 'Name' key as a list. I had hoped that the following
>code would do the trick:
>
>my @names = grep { $_->{Name} } @list;
>
>But it doesn't, because the grep simply matches the hash reference but
>doesn't return the value of the named attribute. Is there a quick way
>to make this work, other than iterating across the list with a
>foreach() statement?


my @names = map $_ -> {Name}, @list;

jue
 
Reply With Quote
 
 
 
 
mrstevegross
Guest
Posts: n/a
 
      11-11-2008
> my @names = map $_ -> {Name}, @list;

Thanks--it works great!

--Steve
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      11-11-2008
mrstevegross <(E-Mail Removed)> wrote:
>> my @names = map $_ -> {Name}, @list;

>
> Thanks--it works great!



If you had paid close attention when you read the documentation
for the function you were using, you could have discovered this
on your own:

perldoc -f grep

...
See also L</map> for a list composed of the results of the BLOCK or EXPR.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to make an array of hashes to a single array with all thevalues of these hashes ? kazaam Ruby 12 09-13-2007 01:30 PM
using hashes as keys in hashes Steven Arnold Ruby 3 11-23-2005 03:25 PM
Hash of hashes, of hashes, of arrays of hashes Tim O'Donovan Perl Misc 5 10-28-2005 05:59 AM
Hashes of Hashes via subs Ben Holness Perl 8 10-08-2003 06:57 AM



Advertisments