Sandra Schlichting
Sandra Schlichting

Reputation: 25986

How can I loop through a Perl array of arrays of hashes?

I would like to print an Array of Arrays of Hashes, so I looked at perldsc, and ended up with

for my $j (0 .. $#aoaoh) {
    for my $aref (@aoaoh) {
    print '"' . join('","', @$aref[$j]), "\"\n";
    }
}

but it doesn't work.

Does anyone know how to do this?

Upvotes: 3

Views: 1763

Answers (4)

draegtun
draegtun

Reputation: 22560

To loop through the whole kit & caboodle:

use 5.012;
use warnings;

my @array = (
    [
        { a => 1, b => 2 },
        { c => 3, d => 4 },
    ],
    [
        { a => 101, b => 102 },
        { c => 103, d => 104 },
    ],
);

for my $root (@array) {
    for my $each_array_of_hashes (@$root) {
        for my $k (keys %{ $each_array_of_hashes } ) {
            say $k, ' => ', $each_array_of_hashes->{$k};
        }
    }
}

Is this what you're after?

Upvotes: 1

Nikhil Jain
Nikhil Jain

Reputation: 8332

Have a look at perlreftut, it will help you, and see the answer below.

  #!/usr/bin/perl

    use strict;
    use warnings;

    my @aoaoh = (
        [
            { a => 1, b => 2 },
            { c => 3, d => 4 },
        ],
        [
            { a => 101, b => 102 },
            { c => 103, d => 104 },
        ],
    );
    for my $j (0 .. $#aoaoh) {
        for my $aref (@{$aoaoh[$j]}) {
            for my $test (keys %{$aref})
            {
               print"$test => ${$aref}{$test}\n";
            }
        }
    }

output:

a => 1
b => 2
c => 3
d => 4
a => 101
b => 102
c => 103
d => 104

Upvotes: 1

Dave Cross
Dave Cross

Reputation: 69244

It works as far as you've gone. Adding some test data to your program gives us:

#!/usr/bin/perl

use strict;
use warnings;

my @aoaoh = (
    [
        { a => 1, b => 2 },
        { c => 3, d => 4 },
    ],
    [
        { a => 101, b => 102 },
        { c => 103, d => 104 },
    ],
);

for my $j (0 .. $#aoaoh) {
    for my $aref (@aoaoh) {
    print '"' . join('","', @$aref[$j]), "\"\n";
    }
}

And running that gives:

$ ./aoaoh 
"HASH(0x9c45818)"
"HASH(0x9c70c48)"
"HASH(0x9c60418)"
"HASH(0x9c70c08)"

So you've successfully navigated the two levels of arrays and you're just left with the hash references to dereference. Something like this perhaps:

#!/usr/bin/perl

use strict;
use warnings;

my @aoaoh = (
    [
        { a => 1, b => 2 },
        { c => 3, d => 4 },
    ],
    [
        { a => 101, b => 102 },
        { c => 103, d => 104 },
    ],
);

for my $j (0 .. $#aoaoh) {
    for my $aref (@aoaoh) {
        # print '"' . join('","', @$aref[$j]), "\"\n";
        for (keys %{$aref->[$j]}) {
            print "$_ -> $aref->[$j]{$_}\n";
        }
    }
}

Which gives:

$ ./aoaoh 
a -> 1
b -> 2
a -> 101
b -> 102
c -> 3
d -> 4
c -> 103
d -> 104

Personally, I'd write it like this as I think it's easier to deal with elements than indexes.

#!/usr/bin/perl

use strict;
use warnings;

my @aoaoh = (
    [
        { a => 1, b => 2 },
        { c => 3, d => 4 },
    ],
    [
        { a => 101, b => 102 },
        { c => 103, d => 104 },
    ],
);

for my $aref (@aoaoh) {
    for my $href (@$aref) {
        for (keys %{$href}) {
            print "$_ -> $href->{$_}\n";
        }
    }
}

Upvotes: 8

gangabass
gangabass

Reputation: 10666

foreach my $aoh (@aoaoh) {

    foreach my $hashref ( @{$aoh} ) {

        foreach my $key ( keys %{$hashref} ) {

            print $key . " => " . $hashref->{$key}, "\n";
        }

        print "#" x 40, "\n";
    }
}

UPDATE: Sorry, it must be array (not array ref)

Upvotes: 1

Related Questions