Loading...
Many time we see that we want to add the many detail's of the user or any other things in our program and we also want to display it when we want but many time it will be become more complicated to understand and put it in the form of the pogram. In this smartbook i will try to explain and become it more easy to understand to you and for that i used the example such as a user and the age of it. I read one defination of the mapping is that Mapping in Solidity acts like a hash table or dictionary in any other language.
what is actually mean the mapping is a particular table in which you assign one default variable with the bool or integer or any other datatype as well. let me explain it with example. we have a mapping in which we assign one address with the integer value.
mapping(address=>uint) public User;
In the above mapping we connected one address with the unsigned integer and we make that mapping public to access anywere within our contract trough the name called as a user. As i tell above we used the example such as a user name and their age we also create one fuction for insert the value in that mapping as well so let's see how can we do that.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract table
{
mapping(uint256 => string) public User;
function adduser() public
{
User[10]="vaibhav";
User[20]="rama";
User[30]="suresh";
User[40]="avinash";
}
}
Here we created one mapping table called as a User In which we use the key and value pair called as a age and the name of the particular person or we consider as a we create one function called as a adduser and add other user as well. After adding the user you can access it with the help of user age.