[Ethereum] Returning 2D array with dynamic sizes from a function

arrayssolidity

This 2D array works when setting the array lenghts with constant values. But i'd prefer to use a dynamic array size:

function funWith2DArray(address[] array1,  address[] array2) public view returns (uint[50][50]) {
    uint[50][50] memory result;
    // ... 
    return result; 
}

This 1D array works, when dynamically setting the array length:

function funWith1DArray(address[] array1,  address[] array2) public view returns (uint[]) {
    uint[] memory result = new uint[](array1.length * 2);
    // ... 
    return result; 
}

How can I do a 2D array with dynamic lengths?:

function funWith2DArray_2(address[] array1,  address[] array2) public view returns (uint[][]) {
    // Not valid syntax
    // uint[][] memory result = new uint[][](array1.length * 2)(array1.length * 2);
    // Not correct either, error: Identifier must be declared a constant
    // uint[array1.length * 2][array1.length * 2] memory result;
    return result; 
}

Note: This function must have the view modifier so I can call it (reading data only) without paying gas.

My current workaround is just cramming all the response data into one array and just knowing where to look to find it (which is okay)

Best Answer

It's not possible yet. See Solidity 0.4.21 FAQ here (search for "dynamic array") -> http://solidity.readthedocs.io/en/develop/frequently-asked-questions.html

It is also not possible to return string[] since technically it's also a 2-dim array.

Related Topic