Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Using Data::Dumper, I have an array refs of hash refs data structure. Whats the best way to iterate over it and print only the "State" key? Using Dump I get the following:
$VAR1 = [ { 'State' => 'Maine', 'Town' => 'Portland' }, { 'State' => 'New Hampshire', 'Town' => 'Concord' } ]; $VAR1 = [ { 'State' => 'Florida', 'Town' => 'Jacksonville' }, { 'State' => 'North Carolina', 'Town' => 'Waynesville' } ];
Thanks

Replies are listed 'Best First'.
Re: printing array ref of hash refs
by mickeyn (Priest) on Oct 25, 2006 at 14:55 UTC
    well, what do you mean by "I have an array refs" ? how do you contain them ?

    the following foreach block example extracts State value for a single ref :

    my $ref = [ { 'State' => 'Maine', 'Town' => 'Portland' }, { 'State' => 'New Hampshire', 'Town' => 'Concord' } ]; foreach ( @{$ref} ) { print $_->{State}."\n"; }
    so to use it on all your refs, you'll need to run this foreach loop on each one of your refs.

    HTH.

    Enjoy,
    Mickey

      Mickey, that did the trick...Thanks!
Re: printing array ref of hash refs
by ikegami (Patriarch) on Oct 25, 2006 at 15:03 UTC
    When dumping an array or a hash, you'll get better output if you dump a reference to the array or to the hash.
    print(Dumper(@array)); -> print(Dumper(\@array)); print(Dumper(%hash)); -> print(Dumper(\%hash)); print(Dumper($a_ref)); -> ok print(Dumper($h_ref)); -> ok

    Now back to the question. You have an array which contains references to arrays (groups??) which contains references to hashes (cities), with key State (and key Town).

    my @array = ( [ { 'State' => 'Maine', 'Town' => 'Portland' }, { 'State' => 'New Hampshire', 'Town' => 'Concord' } ], [ { 'State' => 'Florida', 'Town' => 'Jacksonville' }, { 'State' => 'North Carolina', 'Town' => 'Waynesville' } ] ); foreach my $group (@array) { # which contains refs to arrays foreach my $city (@$group) { # which contains refs to hashes print($city->{State}, "\n"); # with key State. } }

    You probably wanted an AoH instead of an AoAoH.

    my @array = ( { 'State' => 'Maine', 'Town' => 'Portland' }, { 'State' => 'New Hampshire', 'Town' => 'Concord' }, { 'State' => 'Florida', 'Town' => 'Jacksonville' }, { 'State' => 'North Carolina', 'Town' => 'Waynesville' } ); foreach my $city (@array) { # which contains refs to hashes print($city->{State}, "\n"); # with keys State. }
Re: printing array ref of hash refs
by planetscape (Chancellor) on Oct 25, 2006 at 17:32 UTC

    IMHO, one of the best discussions of complex data structures, and how to access their innards , is to be found in Chapter 3. References and complex data structures of perlinter.pdf, available from Perl Training Australia. The OP would probably want the most current version of the course notes, though, which have been reworked into progperl.pdf, available from the same page noted above.

    You might also wish to take a look at Re: How can I visualize my complex data structure?

    HTH,

    planetscape
Re: printing array ref of hash refs
by davorg (Chancellor) on Oct 25, 2006 at 14:57 UTC

    I'm loathe to just give you the answer without seeing any code that you've tried first. But you can probably get a lot of help from the Data Structures Cookbook.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg