in reply to Re: Question about returning array from sub
in thread Question about returning array from sub

Thanks hippo. That indeed did work. But it is still not clear to me. I did declare the array and assign values to it and not references. So when I want to print the array why does it print the array with references.
  • Comment on Re^2: Question about returning array from sub

Replies are listed 'Best First'.
Re^3: Question about returning array from sub
by QM (Parson) on Jul 10, 2015 at 13:00 UTC
    Perl does multidimensional arrays by storing elements of the sub-array in the parent array. So
    $x[0][1] = 12;

    is the syntactic-sugar equivalent of

    $x[0]->[1] = 12;

    (IIRC, in Perl 4, this was the required invocation.)

    And the whole sub-array as

    $x[0] = \@subarray;

    You might also think about it like this:

    @x = ( ["some", "sub", "array"], ["next", "sub", "array] );

    which is making anonymous arrays, and assigning the lot to @x. And these can be nested as deeply as you have memory for (AFAIK, but that would be a good exercise to check).

    Update:

    Yes, it seems that simply nesting empty arrays keeps going until it runs out of memory, starts swapping, and in my case, is killed by the OS.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of