Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic array in Solidity

I want to declare a simple array (dynamic list), one set function to push a string in and one get function which returns all the strings saved in the dynamic array.

I search a lot but not able to find this simple stuff.

like image 928
Jay Vyas Avatar asked Aug 26 '26 03:08

Jay Vyas


1 Answers

Here is my solution, you need experimental ABIEncoderV2 to return array of strings.

pragma solidity ^0.5.2;
pragma experimental ABIEncoderV2;

contract Test {

    string[] array;

    function push(string calldata _text) external {
        array.push(_text);
    }

    function get() external view returns(string[] memory) {
        return array;
    }
}
like image 191
Yegor Zaremba Avatar answered Aug 29 '26 02:08

Yegor Zaremba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!