tags:

views:

95

answers:

4

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?

+1  A: 
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)

gangabass
+1  A: 

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?

/I3az/

draegtun
If you are going to use `say`, please use one of the pragmas that turn it on. Otherwise people will think your example is broken (because it is).
Chas. Owens
re: Chas comment. If you have pre 5.10 version of Perl then look at `Perl::Say` CPAN module. Otherwise amend `say` to `print` and postfix with a newline (ie. `print "some text\n";` as opposed to `say "some text";`
draegtun
@Chas. Owens: Have updated now. Hopefully *people* won't think its now broken because they're not running on perl 5.12 :)
draegtun
Typo: thats `Perl6::Say` - http://search.cpan.org/dist/Perl6-Say/
draegtun
+5  A: 

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";
        }
    }
}
davorg
+1  A: 

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
Nikhil Jain