0
    pragma solidity ^0.5.0;

    contract Testing{

        struct personalInfo {
            uint id;
            string[] colors;
            bool player;
        }

        mapping(uint=> personalInfo) public personals;
        constructor () public {

        }

        function updatePersonalInfo(uint _ID, string memory _color, bool _player) public {
            personalInfo storage updateP = personals[_ID];
            updateP.id = _ID;
            updateP.player = _player;
            updateP.colors.push(_color);
        }
}

Please tell me why _color is not pushing inside the array.?? No error but color array not returned in the output of personals function.

  • 1
    what is the error you are getting? – Aniket Jun 17 at 9:35
  • No error getting and it compile and deployed successfully. but at the time of getting personals function by id...it returns the output of personalInfo structure excluding color array. Can you please try that code in remix so that you can get an clear idea. – Suria Jun 17 at 9:37
  • I think there is no array of string in solidity stackoverflow.com/questions/42716858/string-array-in-solidity – Majd TL Jun 17 at 9:45
  • Thank you @MajdTL I realised that thing. But what is the suggested way of my problem statement.I need that structure like a way.I need to store string array in structure , SO is there any alternative best solution ? and Thanks again. – Suria Jun 17 at 9:48
  • See this for solution: ethereum.stackexchange.com/questions/17312/… – Aniket Jun 17 at 9:56
1

You can get array values by this code:

pragma solidity ^0.5.0;
pragma experimental ABIEncoderV2;

contract Testing{

    struct personalInfo {
        uint id;
        string[] colors;
        bool player;
    }

    mapping(uint=> personalInfo) public personals;
    constructor () public {

    }    function updatePersonalInfo(uint _ID, string  memory _color, bool _player) public {
        personalInfo storage updateP = personals[_ID];
        updateP.id = _ID;
        updateP.player = _player;
        updateP.colors.push(_color);
    }
    function rPersonalInfo(uint id)public view returns(personalInfo memory p){
        return personals[id];
    }
}
  • Thank you so much @Mahesh Rajput ..It works perfectly fine...Can you please explain why it didnt returns from personals() ? – Suria Jun 17 at 10:02
  • because right now solidity does not support string array returning but we can use this experimental version for our testing. – Mahesh Rajput Jun 17 at 10:06
  • It means...it store the value in array but not returning right ? – Suria Jun 17 at 10:11
  • Yes. It is supporting to store array but not returning. – Mahesh Rajput Jun 17 at 10:14
  • thanks again... Hope it will support in future.... – Suria Jun 17 at 10:16

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.