How to map an address with a string array in solidity?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












Goal: I wish to store an array of names with an address in a smart contract. It's more like Key-Value pairs where addresses are paired with a list of type strings.



Can I use mapping to implement this? See below for instance.



mapping(address => strings) userAddress2UserNames;


Or, there is a different approach to it?



Experiments I performed



I got no error when I compiled the above code but when I call a method which returns string from this mapping I got an error stated below.



TypeError: This type is only supported in the new experimental ABI encoder. Use "pragma experimental ABIEncoderV2;" to enable the feature.


I'm not much aware of this version but I am assuming I should not do production level implementation with it because may be it's not stable. (Please correct me if I am wrong)



Please suggest me a way to achieve my goal.










share|improve this question





















  • Currently you cannot use strings like that. A string itself is an array of bytes and you are mapping an array of an array of bytes. Solidity does not have the feature to do that but syntactically it is alright. Since it is a feature that is still being developed that is why you are getting that error. Which version of solidity are you using i.e what is the number beside pragma solidity?
    – R.D
    7 hours ago










  • I'm using 0.4.24
    – Gagan
    6 hours ago










  • So I really want to implement such mapping. What are the other ways of doing it? Structs? 2-d array of type bytes32? I'm quite confused.
    – Gagan
    6 hours ago










  • Try the same code in 0.4.25 or 0.5.0 then you may be able to do it. I don't know anything more sorry.
    – R.D
    6 hours ago














up vote
1
down vote

favorite












Goal: I wish to store an array of names with an address in a smart contract. It's more like Key-Value pairs where addresses are paired with a list of type strings.



Can I use mapping to implement this? See below for instance.



mapping(address => strings) userAddress2UserNames;


Or, there is a different approach to it?



Experiments I performed



I got no error when I compiled the above code but when I call a method which returns string from this mapping I got an error stated below.



TypeError: This type is only supported in the new experimental ABI encoder. Use "pragma experimental ABIEncoderV2;" to enable the feature.


I'm not much aware of this version but I am assuming I should not do production level implementation with it because may be it's not stable. (Please correct me if I am wrong)



Please suggest me a way to achieve my goal.










share|improve this question





















  • Currently you cannot use strings like that. A string itself is an array of bytes and you are mapping an array of an array of bytes. Solidity does not have the feature to do that but syntactically it is alright. Since it is a feature that is still being developed that is why you are getting that error. Which version of solidity are you using i.e what is the number beside pragma solidity?
    – R.D
    7 hours ago










  • I'm using 0.4.24
    – Gagan
    6 hours ago










  • So I really want to implement such mapping. What are the other ways of doing it? Structs? 2-d array of type bytes32? I'm quite confused.
    – Gagan
    6 hours ago










  • Try the same code in 0.4.25 or 0.5.0 then you may be able to do it. I don't know anything more sorry.
    – R.D
    6 hours ago












up vote
1
down vote

favorite









up vote
1
down vote

favorite











Goal: I wish to store an array of names with an address in a smart contract. It's more like Key-Value pairs where addresses are paired with a list of type strings.



Can I use mapping to implement this? See below for instance.



mapping(address => strings) userAddress2UserNames;


Or, there is a different approach to it?



Experiments I performed



I got no error when I compiled the above code but when I call a method which returns string from this mapping I got an error stated below.



TypeError: This type is only supported in the new experimental ABI encoder. Use "pragma experimental ABIEncoderV2;" to enable the feature.


I'm not much aware of this version but I am assuming I should not do production level implementation with it because may be it's not stable. (Please correct me if I am wrong)



Please suggest me a way to achieve my goal.










share|improve this question













Goal: I wish to store an array of names with an address in a smart contract. It's more like Key-Value pairs where addresses are paired with a list of type strings.



Can I use mapping to implement this? See below for instance.



mapping(address => strings) userAddress2UserNames;


Or, there is a different approach to it?



Experiments I performed



I got no error when I compiled the above code but when I call a method which returns string from this mapping I got an error stated below.



TypeError: This type is only supported in the new experimental ABI encoder. Use "pragma experimental ABIEncoderV2;" to enable the feature.


I'm not much aware of this version but I am assuming I should not do production level implementation with it because may be it's not stable. (Please correct me if I am wrong)



Please suggest me a way to achieve my goal.







solidity arrays mapping solidity-best-practices solidity-design-patterns






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 7 hours ago









Gagan

737




737











  • Currently you cannot use strings like that. A string itself is an array of bytes and you are mapping an array of an array of bytes. Solidity does not have the feature to do that but syntactically it is alright. Since it is a feature that is still being developed that is why you are getting that error. Which version of solidity are you using i.e what is the number beside pragma solidity?
    – R.D
    7 hours ago










  • I'm using 0.4.24
    – Gagan
    6 hours ago










  • So I really want to implement such mapping. What are the other ways of doing it? Structs? 2-d array of type bytes32? I'm quite confused.
    – Gagan
    6 hours ago










  • Try the same code in 0.4.25 or 0.5.0 then you may be able to do it. I don't know anything more sorry.
    – R.D
    6 hours ago
















  • Currently you cannot use strings like that. A string itself is an array of bytes and you are mapping an array of an array of bytes. Solidity does not have the feature to do that but syntactically it is alright. Since it is a feature that is still being developed that is why you are getting that error. Which version of solidity are you using i.e what is the number beside pragma solidity?
    – R.D
    7 hours ago










  • I'm using 0.4.24
    – Gagan
    6 hours ago










  • So I really want to implement such mapping. What are the other ways of doing it? Structs? 2-d array of type bytes32? I'm quite confused.
    – Gagan
    6 hours ago










  • Try the same code in 0.4.25 or 0.5.0 then you may be able to do it. I don't know anything more sorry.
    – R.D
    6 hours ago















Currently you cannot use strings like that. A string itself is an array of bytes and you are mapping an array of an array of bytes. Solidity does not have the feature to do that but syntactically it is alright. Since it is a feature that is still being developed that is why you are getting that error. Which version of solidity are you using i.e what is the number beside pragma solidity?
– R.D
7 hours ago




Currently you cannot use strings like that. A string itself is an array of bytes and you are mapping an array of an array of bytes. Solidity does not have the feature to do that but syntactically it is alright. Since it is a feature that is still being developed that is why you are getting that error. Which version of solidity are you using i.e what is the number beside pragma solidity?
– R.D
7 hours ago












I'm using 0.4.24
– Gagan
6 hours ago




I'm using 0.4.24
– Gagan
6 hours ago












So I really want to implement such mapping. What are the other ways of doing it? Structs? 2-d array of type bytes32? I'm quite confused.
– Gagan
6 hours ago




So I really want to implement such mapping. What are the other ways of doing it? Structs? 2-d array of type bytes32? I'm quite confused.
– Gagan
6 hours ago












Try the same code in 0.4.25 or 0.5.0 then you may be able to do it. I don't know anything more sorry.
– R.D
6 hours ago




Try the same code in 0.4.25 or 0.5.0 then you may be able to do it. I don't know anything more sorry.
– R.D
6 hours ago










4 Answers
4






active

oldest

votes

















up vote
4
down vote













How long are the user names? Maybe you can use bytes32 instead of string (each username would be limited up to 32 chars). By doing this, you wouldn't be combining two levels of dynamic arrays, which is not permitted in the current version.






share|improve this answer








New contributor




Patricio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    1
    down vote













    As it is suggested by @Patricio use bytes instead of string.

    At current versions there are many limitations on string use. So better use bytes.

    What you want to achieve is One to many mapping. But mapping are unique and you can't map a key with many values, so you have to use struct in this case.

    Follow the below link they have explained it very well.
    https://ethereum.stackexchange.com/a/38842/24373






    share|improve this answer



























      up vote
      1
      down vote













      The compiler will generate automatically getters for variables declared as public.



      mapping(address => strings) userAddress2UserNames;


      In this case the getter will look like



      function userAddress2UserNames(address _addr, uint _index);


      You have to provide an address and index to the array.






      share|improve this answer



























        up vote
        -1
        down vote













        given link will help you!!



        follow link!!






        share|improve this answer




















        • Link only answers are not recommended, the answer will get stale if the server is down or the page has moved. It is better if you include with your answer the core of idea and use the link to expand on the details.
          – Ismael
          1 hour ago










        Your Answer







        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "642"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        convertImagesToLinks: false,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













         

        draft saved


        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fethereum.stackexchange.com%2fquestions%2f60249%2fhow-to-map-an-address-with-a-string-array-in-solidity%23new-answer', 'question_page');

        );

        Post as a guest






























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        4
        down vote













        How long are the user names? Maybe you can use bytes32 instead of string (each username would be limited up to 32 chars). By doing this, you wouldn't be combining two levels of dynamic arrays, which is not permitted in the current version.






        share|improve this answer








        New contributor




        Patricio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





















          up vote
          4
          down vote













          How long are the user names? Maybe you can use bytes32 instead of string (each username would be limited up to 32 chars). By doing this, you wouldn't be combining two levels of dynamic arrays, which is not permitted in the current version.






          share|improve this answer








          New contributor




          Patricio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.



















            up vote
            4
            down vote










            up vote
            4
            down vote









            How long are the user names? Maybe you can use bytes32 instead of string (each username would be limited up to 32 chars). By doing this, you wouldn't be combining two levels of dynamic arrays, which is not permitted in the current version.






            share|improve this answer








            New contributor




            Patricio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            How long are the user names? Maybe you can use bytes32 instead of string (each username would be limited up to 32 chars). By doing this, you wouldn't be combining two levels of dynamic arrays, which is not permitted in the current version.







            share|improve this answer








            New contributor




            Patricio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            share|improve this answer



            share|improve this answer






            New contributor




            Patricio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            answered 5 hours ago









            Patricio

            411




            411




            New contributor




            Patricio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





            New contributor





            Patricio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            Patricio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.




















                up vote
                1
                down vote













                As it is suggested by @Patricio use bytes instead of string.

                At current versions there are many limitations on string use. So better use bytes.

                What you want to achieve is One to many mapping. But mapping are unique and you can't map a key with many values, so you have to use struct in this case.

                Follow the below link they have explained it very well.
                https://ethereum.stackexchange.com/a/38842/24373






                share|improve this answer
























                  up vote
                  1
                  down vote













                  As it is suggested by @Patricio use bytes instead of string.

                  At current versions there are many limitations on string use. So better use bytes.

                  What you want to achieve is One to many mapping. But mapping are unique and you can't map a key with many values, so you have to use struct in this case.

                  Follow the below link they have explained it very well.
                  https://ethereum.stackexchange.com/a/38842/24373






                  share|improve this answer






















                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    As it is suggested by @Patricio use bytes instead of string.

                    At current versions there are many limitations on string use. So better use bytes.

                    What you want to achieve is One to many mapping. But mapping are unique and you can't map a key with many values, so you have to use struct in this case.

                    Follow the below link they have explained it very well.
                    https://ethereum.stackexchange.com/a/38842/24373






                    share|improve this answer












                    As it is suggested by @Patricio use bytes instead of string.

                    At current versions there are many limitations on string use. So better use bytes.

                    What you want to achieve is One to many mapping. But mapping are unique and you can't map a key with many values, so you have to use struct in this case.

                    Follow the below link they have explained it very well.
                    https://ethereum.stackexchange.com/a/38842/24373







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 4 hours ago









                    Shubham Chadokar

                    607




                    607




















                        up vote
                        1
                        down vote













                        The compiler will generate automatically getters for variables declared as public.



                        mapping(address => strings) userAddress2UserNames;


                        In this case the getter will look like



                        function userAddress2UserNames(address _addr, uint _index);


                        You have to provide an address and index to the array.






                        share|improve this answer
























                          up vote
                          1
                          down vote













                          The compiler will generate automatically getters for variables declared as public.



                          mapping(address => strings) userAddress2UserNames;


                          In this case the getter will look like



                          function userAddress2UserNames(address _addr, uint _index);


                          You have to provide an address and index to the array.






                          share|improve this answer






















                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            The compiler will generate automatically getters for variables declared as public.



                            mapping(address => strings) userAddress2UserNames;


                            In this case the getter will look like



                            function userAddress2UserNames(address _addr, uint _index);


                            You have to provide an address and index to the array.






                            share|improve this answer












                            The compiler will generate automatically getters for variables declared as public.



                            mapping(address => strings) userAddress2UserNames;


                            In this case the getter will look like



                            function userAddress2UserNames(address _addr, uint _index);


                            You have to provide an address and index to the array.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 1 hour ago









                            Ismael

                            13k42345




                            13k42345




















                                up vote
                                -1
                                down vote













                                given link will help you!!



                                follow link!!






                                share|improve this answer




















                                • Link only answers are not recommended, the answer will get stale if the server is down or the page has moved. It is better if you include with your answer the core of idea and use the link to expand on the details.
                                  – Ismael
                                  1 hour ago














                                up vote
                                -1
                                down vote













                                given link will help you!!



                                follow link!!






                                share|improve this answer




















                                • Link only answers are not recommended, the answer will get stale if the server is down or the page has moved. It is better if you include with your answer the core of idea and use the link to expand on the details.
                                  – Ismael
                                  1 hour ago












                                up vote
                                -1
                                down vote










                                up vote
                                -1
                                down vote









                                given link will help you!!



                                follow link!!






                                share|improve this answer












                                given link will help you!!



                                follow link!!







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered 6 hours ago









                                Sunanda Sharma

                                146




                                146











                                • Link only answers are not recommended, the answer will get stale if the server is down or the page has moved. It is better if you include with your answer the core of idea and use the link to expand on the details.
                                  – Ismael
                                  1 hour ago
















                                • Link only answers are not recommended, the answer will get stale if the server is down or the page has moved. It is better if you include with your answer the core of idea and use the link to expand on the details.
                                  – Ismael
                                  1 hour ago















                                Link only answers are not recommended, the answer will get stale if the server is down or the page has moved. It is better if you include with your answer the core of idea and use the link to expand on the details.
                                – Ismael
                                1 hour ago




                                Link only answers are not recommended, the answer will get stale if the server is down or the page has moved. It is better if you include with your answer the core of idea and use the link to expand on the details.
                                – Ismael
                                1 hour ago

















                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fethereum.stackexchange.com%2fquestions%2f60249%2fhow-to-map-an-address-with-a-string-array-in-solidity%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Comments

                                Popular posts from this blog

                                What does second last employer means? [closed]

                                Installing NextGIS Connect into QGIS 3?

                                One-line joke