2

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[];
    } 
4
  • try removing [] in return myArray[]; Commented Jul 30, 2019 at 9:36
  • I removed now its saying "browser/HISTORYMultipleStateMach.sol:22:9: TypeError: Different number of arguments in return statement than in returns declaration. return myArray; ^------------^" @PiotrKamoda Commented Jul 30, 2019 at 9:43
  • how about save Machines in the outer scope (around where you declare myArray) and then do return myArray[Machines]? Commented Jul 30, 2019 at 10:37
  • Welcome to StackOverflow Selen! Can you please format your question? Commented Jul 30, 2019 at 14:32

1 Answer 1

2

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;
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.