How to return mapping list in Solidity? (Ethereum contract)

6londe picture 6londe · Jun 3, 2016 · Viewed 12.6k times · Source

I want to make simple smart contract that has a list, can set item, and can get the list.

Code in solidity:

contract lister {
    mapping(int => string) list;
    int id = 0;

    function getList() returns ( /*HERE*/ ) {
        return list;
    }

    function setItemToList(string str) {
        list[id] = str;
        id++;
    }
}

I want to make getList() return the list, but return type is not compatible. How can I do that?

Answer

bortzmeyer picture bortzmeyer · Jun 5, 2016

Bulk access to lists/arrays/etc is painful in Solidity. You rarely see it in contracts. In your case, a possible solution is to provide a function to access one item, using its index, and to let the caller loops from 0 to id.