Tuesday, April 27, 2010

Create 2D array in perl

If we want to first create space for an two-dimensional array, we first need to create space for a single row, which will have numCols columns:

for($col = 0; $col < $numCols; $col++) {

push @rowMatrix, “0”;

}

Then we need to create a matrix of these single dimension row matrices:

for($row = 0; $row < $numRows; $row++) {

push @matrix, [ @rowMatrix ];

}

Now we have a two dimensional array filled with 0’s with of size [numRows] x [numCols].


No comments:

Post a Comment