Mapping in solidity from zero to hero

By akohad Oct26,2022

[ad_1]

  • Mapping is reference type as array and struct
  • Mapping are one of the most widely used complex data types in solidity
  • Mapping are similar to hash tables or dictionaries in other languages
  • Mappings are hash table that can have key and value
  • Mapping allow you to save data and add key to specify to your data
  • Mapping key save as hash to access value
  • Mapping storing key-value pairs
  • Mapping when we are setting new value ,we also overwritting the old one
  • Mapping syntax mapping(keyDataType=>valueDataType) mappingName
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract mappingTest{
mapping(address=>uint) appleProduct;
}
  • Key type in mapping can be any data type or any contract or enum but structs-arrays-mappings are not allowed
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract mappingTest{
enum appleEnum{ipad}
mapping(appleEnum=>string) appleProduct;
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract mappingKey{
}
contract mappingTest{
mapping(mappingKey=>string) appleProduct;
}
//TypeError: Only elementary types, contract types or enums are //allowed as mapping keys.// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract mappingTest{
struct appleStruct{
uint ipad;
}
mapping(appleStruct=>string) appleProduct;
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract mappingTest{
uint[] appleArray;
//Error not compile
mapping(appleStruct=>string) appleProduct;
}

ValueType in mapping can be any dataType Including complex type struct,array and mapping

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract mappingTest{
enum appleEnum {ipad}
mapping(uint=>appleEnum) appleProduct;
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract mappingValue{
}
contract mappingTest{
mapping(string=>mappingValue) appleProduct;
}

code1

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract mappingTest{
struct appleStruct{
uint ipad;
}
mapping(string =>appleStruct) appleProduct;
}

code2

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
contract mappingStruct {
struct allMovies{
string title;
string director;
}
mapping (uint=>allMovies) movies;function addMovies(uint id,string memory title ,string memory director)public {
movies[id]=allMovies(title, director);
}
function getFilmDetails(uint id)public view returns(allMovies memory){
return movies[id];
}
}

code3

pragma solidity 0.8.7;

contract mappingStruct {

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];
}
}

code1

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract mappingTest{
mapping(string=> uint[]) appleProduct;
}

code2

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
contract mappingStruct {
mapping (uint=>string[]) mappingAndArray;function addToMapping(uint myKey1,string memory word1,string memory word2) public{
mappingAndArray[myKey1]=[word1,word2];
}
function getMappingArray(uint mykey1) public view returns(string [] memory){
return mappingAndArray[mykey1];
}
}

code1

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract mappingTest{
mapping(address =>mapping(string=>uint)) nestedMapping;
}

code2

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract mappingTest{
mapping(address=>mapping(uint=>mapping(string=>bool))) threeMapping;

you can mark state variables of mapping typeas public and solidity creates a getter for you and key type become aparameter for the getter

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract mappingTest{mapping(address =>uint) public appleProduct1;
mapping(address =>uint) internal appleProduct2;
mapping(address =>uint) private appleProduct3;
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.0;
contract addToMapping{
mapping(uint=>string) apple;
function appleProduct(uint appleId,string memory _appleProduct)
public{
apple[appleId]= _appleProduct;
}
function getAppleProduct(uint id) public view returns(string memory){
return apple[id];
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract mappingTest{mapping(string=>uint) age;function addToMapping(string memory _name,uint _age) public{
age[_name]=_age;
}
function getAge(string memory _name) public view returns(uint){
return age[_name];
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract mappingTest{
mapping(address =>string) ChooseAppleProduct;
function setAppleProduct(string memory appleProduct) public {
ChooseAppleProduct[msg.sender]=appleProduct;
}
function getMyChoice()public view returns(string memory){
return ChooseAppleProduct[msg.sender];
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract mappingTest{mapping(uint=>address) member;
uint counter;
function addMember(address _add) public {
counter++;
member[counter]=_add;
}
function getMember(uint id) public view returns(address){
return member[id];
}
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.0;
contract addToMapping{
mapping(uint=>string[]) apple;
function addAppleProduct(uint appleId,string memory appleProduct1,string memory appleProduct2,string memory appleProduct3) public{
apple[appleId]= [appleProduct1,appleProduct2,appleProduct3];
}
function getAppleProduct(uint id) public view returns(string [] memory){
return apple[id];
}
}

msg.sender global variable sender of the message (current call)

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract mappingDelete{
mapping(address=>string) userName;function setUserName(string memory _name) public {
userName[msg.sender]=_name;
}
function getUserName() public view returns(string memory){
return userName[msg.sender];
}
function deleteMappingValue() public{
delete userName[msg.sender];
}
}

Although mapping doesn’t support iteration, there are ways to work around this limitation. The next example illustrates one of the ways to iterate through mapping. Please note that iterating and looping are expensive operations in Ethereum in terms of gas usage and should generally be avoided. In this example, a separate counter is maintained to keep
track of the number of entries stored within the mapping. This counter also acts as the key within the mapping. A local array can be constructed for storing the values from mapping.
A loop can be executed using counter and can extract and store each value from the mapping in the local array,

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract MappingLooping {
mapping (uint => string) Names;
uint counter;
function addtoMapping(string memory apple) public{
counter = counter + 1;
Names[counter]=apple;
}
function getMappingMember() public view returns (string[]
memory) {
string[] memory allAppleProduct = new string[](counter);
for(uint i=1; i<= counter; i++){
allAppleProduct[i - 1] = Names[i];
}
return allAppleProduct;
}
}

New to trading? Try crypto trading bots or copy trading

[ad_2]

Source link

By akohad

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *