Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Adding to hash that contains an array of arrays

by rnewsham (Curate)
on Jan 29, 2014 at 20:16 UTC ( #1072575=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1072575]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2020-11-24 21:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?