in reply to Max dimensions of multi-dimensional array

The reason you cannot find any documentation about the maximum size available for an array is that arrays expand as needed. They are limited only by memory available.

Because each index starts at zero, when you store the elements you specified, the size of your array will expand to 346 x 65536 (if it is not at least that large already).

The solutions you have received do not give the same result. One gives the maximum index that has been used for each dimension. The other gives the current sizes (which are each one greater).

Bill
  • Comment on Re: Max dimensions of multi-dimensional array

Replies are listed 'Best First'.
Re^2: Max dimensions of multi-dimensional array
by AnomalousMonk (Archbishop) on Jul 13, 2016 at 14:22 UTC
    ... arrays expand as needed. ... the size of your array will expand to 346 x 65536 ...

    That's a bit misleading. It implies the allocation of space for a | an orthogonal multi-dimensional array of 346 * 65,536 == 22,675,456 elements. Because Perl multi-dimensional arrays are "ragged", each array "dimension" (update: maybe "vector" would be a better term here) is only allocated the space it actually needs. I count the array specified in the OPed code like this:

    • @array array of 346 elements, mostly undefined;
    • $array[0] reference to an empty array;
    • $array[1] undefined, doesn't exist as an array;
    • $array[2] reference to an array of 65536 elements, only two of which are defined;
    • array elements  @array[3 .. 344] are undefined;
    • $array[345] reference to an array of 11 elements, only one of which is defined.

    By my count, I get a total of 346 + 65,536 + 11 == 65,893 elements, almost all of which are undefined; the array, as it stands, is very sparse.

    Update: Please see Manipulating Arrays of Arrays in Perl (perllol) and Perl Data Structures Cookbook (perldsc).


    Give a man a fish:  <%-{-{-{-<