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

Hello MONKS,

Question: I have an abundance of equal size arrays that need to be associated, having understood 1:1 mapping, for example:

my %myhash = map { $array1[$_] => $array2[$_] } 0 .. $#array1;
Consistent with the above example I would like to use $array1 as a hash key but inside of a more complex Hash of Hashes which would associate $array3, $array4, $array5 ... using the array name as the subkey for the nested hash.

Possibly:

my %HoH = map { $array1[$_] => “array2” => $array2[$_] } 0 .. $#array1 +; %HoH = map { $array1[$_] => “array3” => $array3[$_] } 0 .. $#array1; %HoH = map { $array1[$_] => “array4” => $array4[$_] } 0 .. $#array1; %HoH = map { $array1[$_] => “array5” => $array5[$_] } 0 .. $#array1;
etc. etc. until all the arrays have been associated within the HoH.

I want the final HoH to work something like this:

Foreach @array1 { print $HoH{$array1[$_]}{array5} # array5 associated with array1 print $HoH{$array1[$_]}{array4} # array4 associated with array1 print $HoH{$array1[$_]}{array3} # array3 associated with array1 # etc. etc. }
Is the above code a proficient way of associating all arrays with the array1 keys within the same data structure?

All advice is always greatly appreciated!

Thanks.

Replies are listed 'Best First'.
Re: converting arrays to a complex HoH data structure
by Enlil (Parson) on Jan 06, 2004 at 21:25 UTC
    I think this will work for what you are asking for (I changed the names of the arrays to make it more descriptive as to what was going on):
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @keys = qw(a b c d e f g); my @numbers = qw(1 2 3 4 5 6 7); my @animals = qw(aardvark bird cat dog elephant fish goat); my @boys = qw(andy bart charles david elmer fred george); my @girls = qw(anna bertha charlize daphne ellen frida gina); my %hash = map { $keys[$_] => { numbers => $numbers[$_], animals => $animals[$_], boys => $boys[$_], girls => $girls[$_] } } 0 .. $#keys; print Dumper \%hash; __END__ $VAR1 = { 'e' => { 'girls' => 'ellen', 'boys' => 'elrond', 'numbers' => '5', 'animals' => 'elephant' }, 'c' => { 'girls' => 'charlize', 'boys' => 'charles', 'numbers' => '3', 'animals' => 'cat' }, 'a' => { 'girls' => 'anna', 'boys' => 'andy', 'numbers' => '1', 'animals' => 'aardvark' }, 'g' => { 'girls' => 'gina', 'boys' => 'george', 'numbers' => '7', 'animals' => 'goat' }, 'b' => { 'girls' => 'bertha', 'boys' => 'bart', 'numbers' => '2', 'animals' => 'bird' }, 'd' => { 'girls' => 'daphne', 'boys' => 'david', 'numbers' => '4', 'animals' => 'dog' }, 'f' => { 'girls' => 'frida', 'boys' => 'fred', 'numbers' => '6', 'animals' => 'fish' } };

    -enlil

Re: converting arrays to a complex HoH data structure
by kesterkester (Hermit) on Jan 06, 2004 at 21:19 UTC
    I'd use something like this instead of a HOH:
    my @array1 = ( 5, 6, 7, 8 ); my @array2 = ( 6, 7, 8, 9 ); my @array3 = ( 7, 8, 9,10 ); my @array4 = ( 8, 9,10,11 ); my @array5 = ( 9,10,11,12 ); my %hoa = map { $array1[$_] => [ $array2[$_], $array3[$_], $array4[$_], $array5[$_] ] } 0 .. scalar @array1 - 1; print Dumper \%hoa;
    with output
    $VAR1 = { '8' => [ 9, 10, 11, 12 ], '6' => [ 7, 8, 9, 10 ], '7' => [ 8, 9, 10, 11 ], '5' => [ 6, 7, 8, 9 ] };

    Since your arrays don't have descriptive names, you won't lose any information by putting it into a hash of arrays instead.