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

How do I go about nesting arrays inside of a hash, on the fly... I need to transfer info from an array into a hash, but the value slot has multi values, how many is unknown...

Replies are listed 'Best First'.
Re: nested arrays
by aufrank (Pilgrim) on Aug 14, 2002 at 19:11 UTC

    your question could be more clear, it would be especially helpful if you were to include some code for us to work with. check ybiC's homenode for a lot of good links on asking questions. That said, to put an arrayref into a hash, try:

    use Data::Dumper; my @array1 = qw /foo bar baz/; my @array2 = qw /qux quux quuux/; my %hash; $hash{'array1'} = [@array1]; $hash{'array2'} = [@array2]; print Dumper %hash; print $hash{'array1'}[0];

    Data::Dumper is really useful for examining your data structures, and is part of the core distribution of perl. The last line shows how you would access an individual element of the array. Also, check out Perl Datastructures Cookbook for more information on creating complex data structures, and perlref for information on using references in perl.

    hth,
    --au

Re: nested arrays
by Zaxo (Archbishop) on Aug 14, 2002 at 18:54 UTC
Re: nested arrays
by the_slycer (Chaplain) on Aug 14, 2002 at 18:58 UTC
    Sounds like you want an HoA.
    You can do this using array references:
    my @array1 = ('1','2','3'); my @array2 = ('4','5','6'); my %hash = ( array1 => \@array1, array2 => \@array2 ); foreach (keys %hash) { foreach (@{$hash{$_}}) { print } }
    That should give you the general idea. You can also see the manpage for perlref.