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
    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. – Rob Hitchens Oct 19 '22 at 21:12

1 Answers1

2

You can try this:

pragma solidity 0.8.7;

contract nestmap {

struct Person{
    uint256 age;
    string name;
}

mapping (uint256 =&gt; 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];
}

}

Yongjian P.
  • 4,170
  • 1
  • 3
  • 10