in reply to Writing an array inside an array

I am a bit confused as to exactly what you are trying to do, (and things like where $graph comes from), but it appears to me that you are building a 2-D matrix. These are built in Perl the same way that it is done in C except that its a lot easier! Each "row" is a reference to a 1-D array. So this is an "array of array". In this sort of structure everything is a reference until you get to the last dimension.

The natural thing to do is to use syntax like: $twoDmatrix[$row][$col] to access each element of the matrix.

I wrote a simple example for you. Below @twoDmatrix is an array which will wind up with references to other arrays. You don't have to know how many "rows" or "columns" that there are going to be in advance, you just start using it and Perl will figure it out! In C you'd have to allocate memory and take care of some other details yourself, but not with Perl.

Anyway there is no need to make some temporary "sub array" and then stick a reference to it into @twoDmatrix, go ahead and just use the [row][col] coordinate. By the way DO NOT use perl reserved names (like sub) for variable names! Because that creates lots of opportunities for confusion and errors.

In your main code, I would work with @twoDmatrix instead of using a reference to it because it is easier and takes out one layer of complication. To pass @twoDmatrix to a subrouinte, take a reference to it in the calling line like: graphfunc(\@twoDmatrix); Note that this is how @twoDmatrix is passed to Dumper below - a very handy function for printing data structures while you are debugging. As you can see @twoDmatrix is indeed an AoA (array of array).

Update:
Updated code to show how to send a "row" into a sub, remember that @twoDmatrix is an array of references to arrays. So just: somefunc ($twoDmatrix[1]);sends a reference to row 1 into somefunc (see how I called Dumper below). I guess while I'm at it, I should mention that the matrix does not have to be square or even have the same number of columns in each row, however I would guess that in your case, each row will have same number of columns although maybe rows!=columns, which is just fine.

#!/usr/bin/perl -w use strict; use Data::Dumper; my @list = (7,15,2); my @twoDmatrix =(); # the = () not really needed here. # take off "my" if you want to "clear" the matrix # later at some point, just: @twoDmatrix = (); my $rows = @list; # evaluation of @list in scalar context # yields 3, the number of elements # just replicated @list into multiple rows with # simple offset to make each row different # result is square matrix (num rows = num cols) my ($row, $col); for ($row=0; $row<$rows; $row++) { for ($col=0; $col<@list; $col++) { $twoDmatrix[$row][$col] = $list[$col]+ $row+1; } } print Dumper \@twoDmatrix; print "element [1][2] is $twoDmatrix[1][2] \n"; print Dumper $twoDmatrix[1]; #twoDmatrix[1] is #already a reference! no \ needed. __END__ $VAR1 = [ [ 8, 16, 3 ], [ 9, 17, 4 ], [ 10, 18, 5 ] ]; element [1][2] is 4 $VAR1 = [ 9, 17, 4 ];