I think I figured out what you're trying to do here. If I'm off a bit, try to use this as a base to build from. You might want to take a look at perlref, perlreftut, and perldsc to learn more about references and complex data structures.
The example below should get you started. Since it appears you want to use a subroutine to add the array to the hash, I've coded it that way.
HTHuse strict; use warnings; use Data::Dumper; my %hash; my @array1 = qw( one two three ); my @array2 = qw( four five six ); my @array3 = qw( seven eight nine ); add_arrayref_to_hashref( \@array1, \%hash, 'key1' ); print "\nafter adding array1 using key1:\n", Dumper( \%hash ); add_arrayref_to_hashref( \@array2, \%hash, 'key1' ); print "\nafter adding array2 using key1:\n", Dumper( \%hash ); # getting a bit fancier here... my $array3ref = \@array3; add_arrayref_to_hashref( $array3ref, \%hash, ${ $array3ref }[1] ); print "\nafter adding array3 using ${ $array3ref }[1]:\n", Dumper( \%h +ash ); sub add_arrayref_to_hashref { my ( $arrayref, $hashref, $key ) = @_; push( @{ ${ $hashref }{$key} }, $arrayref ); }
OUTPUT:
after adding array1 using key1: $VAR1 = { 'key1' => [ [ 'one', 'two', 'three' ] ] }; after adding array2 using key1: $VAR1 = { 'key1' => [ [ 'one', 'two', 'three' ], [ 'four', 'five', 'six' ] ] }; after adding array3 using eight: $VAR1 = { 'key1' => [ [ 'one', 'two', 'three' ], [ 'four', 'five', 'six' ] ], 'eight' => [ [ 'seven', 'eight', 'nine' ] ] };
Update: Ack! I see davidj posted a very similar response while I was composing mine. His uses a slice with your actual data, while mine is more of a general example. Use whichever helps, and enjoy using refs! Extra credit: Instead of passing 3 arguments, you could eliminate the key and simply pass references to the hashref and the desired arrayref, but I'll leave that as an exercise for the reader...
In reply to Re: Loading of Complex Data Structures
by bobf
in thread Loading of Complex Data Structures
by vegasjoe
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |