in reply to Dereferencing array of hashes

Building upon LanX's reply, perhaps the following will be helpful. Also note that it's not necessary to use a C-style loop here:

use strict; use warnings; my @AoH = ( { a => "a1", b => "b1", c => "c1", }, { d => "d1", e => "e1", f => "f1", }, { g => "g1", h => "h1", i => "i1", }, ); for my $i ( 0 .. $#AoH ) { for my $key ( keys %{ $AoH[$i] } ) { print "$key => $AoH[$i]{$key}\n"; } print "\n"; }

Output:

c => c1 a => a1 b => b1 e => e1 d => d1 f => f1 h => h1 g => g1 i => i1

Replies are listed 'Best First'.
Re^2: Dereferencing array of hashes
by Not_a_Number (Prior) on Nov 11, 2013 at 20:27 UTC
    ...it's not necessary to use a C-style loop here

    Indeed. In fact I'd simplify things even further by not using an array index at all:

    for my $item ( @AoH ) { for my $key ( keys %$item ) { print "$key => $item->{ $key }\n"; } }

      Oh, this is smart++! Am glad you added this comment!

        Thanks. But Laurent_R beat me to it by a clear 30 seconds minutes. Upvote him instead.

        Update: Yes, obviously, minutes not secconds. Thanks Laurent_R and AnomalousMonk!