"Variable Depth" Problem


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

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

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.