Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Extract keys from an Array of Hashes

by bradcathey (Prior)
on Dec 16, 2010 at 17:02 UTC ( #877524=perlquestion: print w/ replies, xml ) Need Help??
bradcathey has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monasterians,

It's easy enough to get the keys in a hash, but what about an array of hashes? All I really need are the keys from just one of the array elements, because the way I'm using the AoH, all the keys are the same in each element of the array.

It would look something like this:

my @AoH = ({'name' => 'Barney', 'age' => '34'},{'name' => 'Fred', 'age +' => '36'},); my %hash = $AoH[0]; print keys %hash; __OUTPUT__ name, age

Of course, that barfs. Is there a way to do this? TIA

—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

Comment on Extract keys from an Array of Hashes
Download Code
Re: Extract keys from an Array of Hashes
by BrowserUk (Pope) on Dec 16, 2010 at 17:07 UTC
    print keys %{ $AoH[0] };

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Brilliant!

      I was close, but that doesn't count.

      Stuck them in an array for good measure:

      my @keys = keys %{ $AoH[0] };

      Thanks!

      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot

      And for when the keys are all different:

      for ( 0 .. $#AoH ) { push @keys, keys %{ $AoH[$_] }; }
      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
Re: Extract keys from an Array of Hashes
by toolic (Chancellor) on Dec 16, 2010 at 17:17 UTC
Re: Extract keys from an Array of Hashes
by eff_i_g (Curate) on Dec 16, 2010 at 18:00 UTC
    This isn't exactly what you want, but it (Data::Rmap) is new to my toolbox and quite handy if your structure becomes more mangled:
    use strict; use warnings; use Data::Rmap qw(:all); my @AoH = ( {'name' => 'Barney', 'age' => '34'}, {'name' => 'Fred', 'age' => '36'}, {'name' => { first => 'John', last => 'Doe' }, age => '40' } ); rmap_hash { print join "\n" => keys %$_; print "\n"; } @AoH;
    name age name age name age first last
Re: Extract keys from an Array of Hashes
by aartist (Scribe) on Dec 16, 2010 at 22:27 UTC
    See, how data structure in Perl works. It will be helpful a lot.
    What you have is.
    my %hash = $AoH[0]; print keys %hash;
    You can just combine these too.
    print keys %hash = print keys %{ hash } = print keys %{ $AoH[0] }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://877524]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2014-08-21 16:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (136 votes), past polls