0

this code require two input(_Id,_name) to get age i need to get name and age from input id only

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;
contract nestmap
{
    mapping(uint256=>mapping(string=>uint256)) public User;
    function adduser(uint256 _Id,string memory _name, uint256 _age)public {
         User[_Id][_name]=_age;
    }
    
}
function user(uint256 _Id,string memory _name)public view returns(uint256)
    {
        return User[_Id][_name];
    }
1
  • 1
    Your nested mapping describes a data structure where each id has a large number of string keys, and each of those maps to a uint256, so probably not what you want. It's roughtly key1 => key2 => value, hence two keys are requested. I voted for Yongjian's answer. That's probably what you want. Oct 19, 2022 at 21:12

1 Answer 1

2

You can try this:

pragma solidity 0.8.7;

contract nestmap {

    struct Person{
        uint256 age;
        string name;
    }

    mapping (uint256 => Person) public User;

    function adduser(uint256 _Id, uint256 _age, string memory _name) public {
         User[_Id].age = _age;
         User[_Id].name = _name;
    }

    function user(uint256 _Id) public view returns(Person memory) {
        return User[_Id];
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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