How can I go through a Perl array of hash arrays?

I would like to print an array of hash arrays, so I looked at perldsc and ended up with

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

but that will not work.

Does anyone know how to do this?

+3
source share
4 answers

It works as far as you are 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 it works, which gives:

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

So, you successfully navigated through two levels of arrays, and you just left the hash links for dereferencing. Something like this is possible:

#!/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";
        }
    }
}

What gives:

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

Personally, I would write it as it seems to me, it is easier to deal with elements than with 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";
        }
    }
}
+8
source
foreach my $aoh (@aoaoh) {

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

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

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

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

UPDATE: , (not array ref)

+1

:

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};
        }
    }
}

, ?

+1

perlreftut, , . .

  #!/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";
            }
        }
    }

:

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

Source: https://habr.com/ru/post/1765072/


All Articles