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

Discussion in 'Perl Misc' started by mrstevegross, Nov 11, 2008.

  1. mrstevegross

    mrstevegross Guest

    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
     
    mrstevegross, Nov 11, 2008
    #1
    1. Advertising

  2. mrstevegross <> 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
     
    Jürgen Exner, Nov 11, 2008
    #2
    1. Advertising

  3. mrstevegross

    mrstevegross Guest

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

    Thanks--it works great!

    --Steve
     
    mrstevegross, Nov 11, 2008
    #3
  4. mrstevegross <> 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/"
     
    Tad J McClellan, Nov 11, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Holness

    Hashes of Hashes via subs

    Ben Holness, Oct 5, 2003, in forum: Perl
    Replies:
    8
    Views:
    572
    Ben Holness
    Oct 8, 2003
  2. Steven Arnold

    using hashes as keys in hashes

    Steven Arnold, Nov 23, 2005, in forum: Ruby
    Replies:
    3
    Views:
    168
    Mauricio Fernández
    Nov 23, 2005
  3. kazaam
    Replies:
    12
    Views:
    279
    Matthias Wächter
    Sep 13, 2007
  4. Neela megha shyam Chivukula

    On Hashes - How the hashes printing works?

    Neela megha shyam Chivukula, May 27, 2009, in forum: Ruby
    Replies:
    4
    Views:
    249
    Markus Schirp
    May 28, 2009
  5. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    217
Loading...

Share This Page