# "Variable Depth" Problem

*From*: none <none@xxxxxxxxx>*Date*: Mon, 05 Apr 2010 23:51:34 GMT

Hi

Sorry if this is a FAQ, but I don't even know the correct terminology to

Google.

In general, how do you solve the "variable dimension" problem? I

understand that some languages support arbitrary-dimension arrays

intrinsically, as in Perl:

my $x;

$x[5][5][5][5][5][5] = 5;

And I understand that you can create similar behavior in more strict

languages, like C++, by creating an array class whose members can also be

arrays, thereby allowing array-of-array-of-array-etc...

But how do you go about writing code to deal with such objects? For

example, if I knew that my array was three levels deep, I could write the

following:

ThreeLevelArray A;

int x, y, z;

for (x = 0; x < A.size() x++)

{

for (y = 0; y < A[x].size(); y++)

{

for (z = 0; z < A[x][y].size(); z++)

{

A[x][y][z] = x + y + z;

}

}

}

But how do you get a variable number of nested for-loops, or the logical

equivalent of that? I'm really just looking for general advice on how to

deal with variable-dimension arrays. In the past, I've run into the

problem only a handful of times, and in each case I was able to do

something that worked but was very un-elegant and required heavy

assumptions about what the nature of the array would be.

Thanks in advance.

.

**Follow-Ups**:**Re: "Variable Depth" Problem***From:*Andrew Poelstra

**Re: "Variable Depth" Problem***From:*Richard Heathfield

**Re: "Variable Depth" Problem***From:*Kai-Uwe Bux

- Prev by Date:
**Re: repeating elements in an array** - Next by Date:
**Re: "Variable Depth" Problem** - Previous by thread:
**repeating elements in an array** - Next by thread:
**Re: "Variable Depth" Problem** - Index(es):