How can I return the mapped list when writing Ethereum Contract?

Asked 2 years ago, Updated 2 years ago, 129 views

I am working on a very simple smart contract that can save the value and get it back as a list. The code is as below.

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 return the list from the getList function, but it doesn't work. How can I return the list?

Source: https://stackoverflow.com/questions/37606839/how-to-return-mapping-list-in-solidity-ethereum-contract This question is authorized to change the same condition (Available under the https://creativecommons.org/licenses/by-sa/3.0/deed.ko ) license.

ethereum solidity

2022-09-22 19:22

2 Answers

It is difficult to access large-sized lists, arrangements, etc. in Solidity. You probably haven't seen much of it in other contracts. In the case of the questioner's code, it is possible to create a function that uses the index to access a single value, and to access all indexes using repetitive statements.


2022-09-22 19:22

If you put public in front of the list, you will be able to access it from the getList.

mapping(int => string) public list;


2022-09-22 19:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.