http://qs321.pair.com?node_id=1072575


in reply to Adding to hash that contains an array of arrays

Here is some code that I think produces the data structure you want. However it sounds like there may be a better structure or approach to achieve the desired result. Perhaps if you provide more of your code and a clearer description of what you are trying to achieve we may be able to offer more helpful suggestions.

use strict; use warnings; use Data::Dumper; my %hash; for my $i ( 1 .. 3 ) { my @array; for my $j ( 1 .. 3 ) { push @array, $j; } push @{ $hash{ID} }, \@array; } print Dumper( %hash );
Output $VAR1 = 'ID'; $VAR2 = [ [ 1, 2, 3 ], [ 1, 2, 3 ], [ 1, 2, 3 ] ];

Replies are listed 'Best First'.
Re^2: Adding to hash that contains an array of arrays
by eppo (Novice) on Jan 29, 2014 at 20:34 UTC

    What i'm trying to do is this, I have an sql table, in the table, there are 2 columns that contain user id's. I'm trying to collect the data for each of the id's. this data was going to be written to an xlsx file. the data needed to create the xlsx file is an array of arrays. so i was thinking about making a hash, that contains each of the id's, and then for each of those id's it points to an array of arrays that I can use to create the spreadsheet. Is there some other way that would be more elegant to get this done?

      Where you get the data from is irrelevant, the "push" demonstrated by both moritz and mewsham manipulates your data structure in exactly the way you're asking for.

      It adds an array reference (what an array of arrays really contains) to the array in your hash.

      -- FloydATC

      Time flies when you don't know what you're doing