Loading...
As we see when we store someting in to your hard disk of your computer or the laptop.we always save the similar contained in the in the same folder and give that folder a specific name always.As you thing that how you assign the similar contain of the particular user assign with the specific id or the identity with the help of the programming language.
what i am saying is that if in a program we have the user name and it's age and we want to store this information with the help of the specific id through which other's can fetch it with the help of that id only. This concept is know as the double mapping in the solidity today we are learning this concept with the help of one example.
So let's get started....
// 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;
}
}
here we created one contract called as the nested map In this contract we created one nested mapping whis is called as a User basically. In that mapping we connected two mapping one is for saving the information of the id of the particular use another is stroing the name and the age of the particular user. At last we created one fuction through which we add the user details. Many people get confuse that we put the value in that mapping but how can we return it .
function user(uint256 _Id,string memory _name)public view returns(uint256)
{
return User[_Id][_name];
}
in the above mapping introducing in the contract we created one function that should retun the age of the user while inputting the id and the name of the user.Like this way you can return the value within the mapping easily.