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

for my $family ( sort keys %HoH ) { print "$family: "; for my $role ( keys %{ $HoH{$family} } ) { print "$role=$HoH{$family}{$role} "; } print "\n"; print "$family: @{ $HoA{$family} }\n"; }

The %HoA and the %HoH have the same keys, yet I get the error:
Can't use an undefined value as an ARRAY reference

in the line:
print "$family: @{ $HoA{$family} }\n";

Replies are listed 'Best First'.
Re: What is wrong in this syntax?
by Athanasius (Archbishop) on Jun 30, 2014 at 02:14 UTC

    Works fine for me:

    #! perl use strict; use warnings; my %HoH = ( Flintstone => { Father => 'Fred', Mother => 'Wilma', }, Simpson => { Father => 'Homer', Mother => 'Marge', }, ); my %HoA = ( Flintstone => [ Father => 'Fred', Mother => 'Wilma', ], Simpson => [ Father => 'Homer', Mother => 'Marge', ], ); for my $family (sort keys %HoH) { print "$family: "; for my $role (keys %{ $HoH{$family} }) { print "$role=$HoH{$family}{$role} "; } print "\n"; print "$family: @{ $HoA{$family} }\n"; }

    Output:

    12:09 >perl 928_SoPW.pl Flintstone: Father=Fred Mother=Wilma Flintstone: Father Fred Mother Wilma Simpson: Father=Homer Mother=Marge Simpson: Father Homer Mother Marge 12:09 >

    You will need to show the exact contents of %HoH and %HoA prior to the for loop. (Use Data::Dumper or Data::Dump.)

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: What is wrong in this syntax?
by Laurent_R (Canon) on Jun 30, 2014 at 06:37 UTC
    It seems that it should work, unless your data structure is not exactly what you think. Please show a dump of your data structures (using Data::Dumper or an equivalent package or the x command of the Perl debugger).
      Nah, sorry for that, it actually works fine, it was some stupid mistake of me :)
Re: What is wrong in this syntax?
by Anonymous Monk on Jun 30, 2014 at 02:05 UTC
    I just tested it and the opposite thing actually works, i.e.
    for my $family ( sort keys %HoA ) { print "$family: "; for my $role ( keys %{ $HoH{$family} } ) { print "$role=$HoH{$family}{$role} "; } #print "\n"; print "@{ $HoA{$family} }\n"; }

    Now I am more confused...