tamaguchi has asked for the wisdom of the Perl Monks concerning the following question:

I have the following structure: I guess this is an array of hash arrays, right?
my @ahoa=( {1=> ['A'=>1.1, 'B'=>10.1, 'C'=>100.1], 2=> ['A'=>2.1, 'B'=>20.1, 'C'=>200.1], 3=> ['A'=>3.1, 'B'=>30.1, 'C'=>300.1]}, {4=> ['A'=>4.1, 'B'=>40.1, 'C'=>400.1], 5=> ['A'=>5.1, 'B'=>50.1, 'C'=>500.1], 6=> ['A'=>6.1, 'B'=>60.1, 'C'=>600.1]}, {7=> ['A'=>7.1, 'B'=>70.1, 'C'=>700.1], 8=> ['A'=>8.1, 'B'=>80.1, 'C'=>800.1], 9=> ['A'=>9.1, 'B'=>90.1, 'C'=>900.1]}, );
I have problems with printing this thing could you help me please?

Replies are listed 'Best First'.
Re: array of hash arrays, right?
by GrandFather (Saint) on Aug 01, 2006 at 11:22 UTC

    It's not clear what you want, but the following may give you some interesting ideas:

    use strict; use warnings; my @ahoa=( {1=> ['A'=>1.1, 'B'=>10.1, 'C'=>100.1], 2=> ['A'=>2.1, 'B'=>20.1, 'C'=>200.1], 3=> ['A'=>3.1, 'B'=>30.1, 'C'=>300.1]}, {4=> ['A'=>4.1, 'B'=>40.1, 'C'=>400.1], 5=> ['A'=>5.1, 'B'=>50.1, 'C'=>500.1], 6=> ['A'=>6.1, 'B'=>60.1, 'C'=>600.1]}, {7=> ['A'=>7.1, 'B'=>70.1, 'C'=>700.1], 8=> ['A'=>8.1, 'B'=>80.1, 'C'=>800.1], 9=> ['A'=>9.1, 'B'=>90.1, 'C'=>900.1]}, ); print dumpRef(\@ahoa); sub dumpRef { my $ref = shift; if ('ARRAY' eq ref $ref) { my $str = ''; $str .= dumpRef ($_) for @$ref; return "$str\n"; } elsif ('HASH' eq ref $ref) { my $str = ''; $str .= " $_ => " . dumpRef ($ref->{$_}) for sort keys %$ref; return "$str\n"; } elsif (length ref $ref) { return 'Panic - got a ' . (ref $ref) . ' and can\'t deal with +it.'; } else { return " $ref"; } }

    Prints:

    1 => A 1.1 B 10.1 C 100.1 2 => A 2.1 B 20.1 C 200.1 3 => A 3.1 B 30.1 C 300.1 4 => A 4.1 B 40.1 C 400.1 5 => A 5.1 B 50.1 C 500.1 6 => A 6.1 B 60.1 C 600.1 7 => A 7.1 B 70.1 C 700.1 8 => A 8.1 B 80.1 C 800.1 9 => A 9.1 B 90.1 C 900.1

    DWIM is Perl's answer to Gödel
Re: array of hash arrays, right?
by dorward (Curate) on Aug 01, 2006 at 11:05 UTC

    I guess this is an array of hash arrays, right?

    It is an array of hashrefs. Each value in the hashrefs is an arrayref. Each arrayref uses (for some reason) fat commas (=>) and has data that suggests it should really be a hashref.

    I have problems with printing this thing could you help me please?

    print @ahoa;

    That probably won't give you what you want, but your question wasn't very specific (and you failed to provide the code your wrote to try to achieve your goal). You should probably have a read of the first three items listed under "Tutorials" in perldoc perl.

Re: array of hash arrays, right?
by syphilis (Archbishop) on Aug 01, 2006 at 11:42 UTC
    If you like nested 'for' loops:
    for my $h(@ahoa) { for (keys(%$h)){ print "$_ => "; print "@{$h->{$_}}\n"; } print "\n"; }
    Slightly more verbose, but perhaps easier for you to follow:
    for my $h (@ahoa) { my %deref = %$h; for my $k (keys(%deref)) { print "$k=> "; my @deref = @{$deref{$k}}; print "@deref\n"; } print "\n"; }
    Hmmm .... perhaps it's not any easier to follow.

    Cheers,
    Rob
      I have done it in the following way:
      #!/usr/bin/perl -w use strict; my @ahoa=( {1=> {'A'=>1.1, 'B'=>10.1, 'C'=>100.1}, 2=> {'A'=>2.1, 'B'=>20.1, 'C'=>200.1}, 3=> {'A'=>3.1, 'B'=>30.1, 'C'=>300.1} }, {4=> {'A'=>4.1, 'B'=>40.1, 'C'=>400.1}, 5=> {'A'=>5.1, 'B'=>50.1, 'C'=>500.1}, 6=> {'A'=>6.1, 'B'=>60.1, 'C'=>600.1} }, {7=> {'A'=>7.1, 'B'=>70.1, 'C'=>700.1}, 8=> {'A'=>8.1, 'B'=>80.1, 'C'=>800.1}, 9=> {'A'=>9.1, 'B'=>90.1, 'C'=>900.1} }, ); for(my $i=0; $i<(scalar(@ahoa)); $i++) { print "i: $i\n"; print "\n"; for my $alphakey (sort {$a <=> $b} keys %{$ahoa[$i]}) { print "\t AK $alphakey\n"; for my $betakey (sort {$a cmp $b} keys %{$ahoa[$i]{$alphakey}} +) {print "\t\t $betakey => $ahoa[$i]{$alphakey}{$betakey}\n";} } print "\n"; }
      thank you all for responding.
        Also remember to change the name from "@ahoa" to something that doesn't conflict with the structure of the thing, since it's now an Array of Hashes of Hashes, or a AoHoH for short.
Re: array of hash arrays, right?
by aufflick (Deacon) on Aug 01, 2006 at 12:55 UTC
    If you just want to print it out to visualise the data for exploration, debugging etc., you don't need to go to the trouble of exploding it yourself - just use Data::Dumper which can print (nearly) any Perl data structure:
    use strict; use Data::Dumper; my @ahoa=( ... ); print Dumper \@ahoa;

      Alternately consider using YAML (or YAML::Syck).

      use YAML; my @aohoh = @{ YAML::Load( <<EOT ) }; --- #YAML:1.0 - 1: { A: 1.1, B: 10.1, C: 100.1 } 2: { A: 2.1, B: 20.1, C: 200.1 } 3: { A: 3.1, B: 30.1, C: 300.1 } - 4: { A: 4.1, B: 40.1, C: 400.1 } 5: { A: 5.1, B: 50.1, C: 500.1 } 6: { A: 6.1, B: 60.1, C: 600.1 } - 7: { A: 7.1, B: 70.1, C: 700.1 } 8: { A: 8.1, B: 80.1, C: 800.1 } 9: { A: 9.1, B: 90.1, C: 900.1 } EOT print YAML::Dump( \@aohoh ), "\n";

      Update: Added use line; I was using perl -MYAML testing but the snippet won't run as is without it.