in reply to Dereferencing array of hashes

Also note that it's not necessary to use a C-style loop...

Yes, sure, but you don't even need a $i variable to loop on the array and make a real Perl loop using directly the reference. This makes the dereferencing simpler.

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 $hashref (@AoH) { print "$_ => $$hashref{$_}\n" for (keys %$hashref); }
which will print:
$ perl aoh.pl 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 AnomalousMonk (Archbishop) on Nov 11, 2013 at 20:17 UTC

    monkini: The critical thing to remember here is that a so-called "array of hashes" (an @AoH) is actually an array of hash references, i.e., every element is already a hash reference. You may as well use those references as such in a Perl-style for-loop as shown by Laurent_R (although I would have used the arrow de-referencing notation  $hashref->{$_} instead of the  $$hashref{$_} form).

      (although I would have used the arrow de-referencing notation $hashref->{$_} instead of the $$hashref{$_} form)

      Yes, right, that's actually what I would probably do also given the time. I typed my post in a rush before leaving for something else and used the dereferencing notation I still know best, but I have been trying in the last months to use more and more the arrow dereferencing notation, which is somewhat clearer. I am now using systematically the arrow notation for code refs and am trying to do it more and more for hash or array refs.