0

I have a mapping

mapping (address => string[]) myMap;

How can I return the list of all the strings associated to a given address?

I know that in Solidity one can't return an array of strings, but what I would need to do is like

function get(address _addr) public returns (string[]){
       return myMap[_addr];
}
2

You will be able to do this if you change the string for bytes32 and make the array fixed in length. For instance, for an array of 10 elements.

mapping (address => bytes32[10]) myMap;
function get(address _addr) public returns (bytes32[10]){
   return myMap[_addr];
}

The issue is that you will need to encode your string as bytes32.

Another option is to activate the experimental ABI encoder in solidity using:

pragma experimental ABIEncoderV2;
pragma solidity 0.4.24;

    contract mycontract{

        mapping (address => string[]) myMap;

        function get(address _addr) public returns (string[]){
           return myMap[_addr];
        }

    }

This is experimental and is not recommended for production.

Hope this helps

  • Thank you very much for your answer. How can I limit the size of the string array? In my case, I have that the string array contains fixed length strings (of 32 bytes). Thank you again – Bruce Wayne Jun 19 '18 at 15:21
  • I am sorry, my answer on the string was incorrect, I updated it, take a look and let me know. – Jaime Jun 19 '18 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.