Selena
Selena

Reputation: 23

How to return a nested array on solidity

I am trying to return nested array on solidity error massage says

"browser/HISTORYMultipleStateMach.sol:22:16: TypeError: Index expression cannot be omitted. return myArray[]; ^-------^" "browser/HISTORYMultipleStateMach.sol:22:16: TypeError: Index expression cannot be omitted. return myArray[]; ^-------^" Can someone tell me what is wrong? Thank you enum State{ A, B, C }

    State[] curState;
    State[][] myArray;

    uint i=0;
    constructor(uint Machines)public{
        for(i=0;i<Machines;i++){
            curState.push(State.A);
            myArray.push(curState);
        }enter code here
    }


    function historyOfStateMachine() public{
        return myArray[];
    }


   function historyOfStateMachine() public{
        return myArray[];
    } 

Upvotes: 2

Views: 1005

Answers (1)

Francis Eytan Dortort
Francis Eytan Dortort

Reputation: 1447

To return the full array, you should remove [] in return myArray[];

Furthermore, it is not yet possible to return two levels of dynamic arrays.

As of version 0.4.19 of solidity, you could activate experimental support for arbitrarily nested arrays using the directive pragma experimental ABIEncoderV2;. In which case your code would be as follows:

pragma solidity ^0.4.19;
pragma experimental ABIEncoderV2;

contract MyContract {
    enum State{ A, B, C }

    State[] curState;
    State[][] myArray;

    uint i=0;

    constructor(uint Machines)public{
        for(i=0;i<Machines;i++){
            curState.push(State.A);
            myArray.push(curState);
        }
    }

    function historyOfStateMachine() public view returns (State[][]) {
        return myArray;
    }

}

Upvotes: 2

Related Questions