xxxxxxxxxx
mapping(datatype(KEY) => datatype(VALUE)) <Access Specifier> <MAPPING VARIABLE DECLARATION NAME>;
xxxxxxxxxx
pragma solidity ^0.5.0;
contract LedgerBalance
{
mapping(address => uint) public balances;
function updateBalance(uint newBalance) public {
balances[msg.sender] = newBalance;
}
}
xxxxxxxxxx
contract Mappings{
mapping(uint=> string) public colors;
constructor(){
colors[1]= "Red";
colors[2]="Yellow";
}
//mapping store data in key value pair here we have colors which contain
//id with color name
}
xxxxxxxxxx
// Solidity program to
// demonstrate mapping
pragma solidity ^0.4.18;
// Defining contract
contract mapping_example {
//Defining structure
struct student
{
// Declaring different
// structure elements
string name;
string subject;
uint8 marks;
}
// Creating a mapping
mapping (
address => student) result;
address[] public student_result;
}
xxxxxxxxxx
mapping(keyType => valueType)mappingName
Example
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Mapping {
// Mapping from address to uint
mapping(address => uint) balance
function get(address _addr) public view returns (uint) {
// Mapping always returns a value.
// If the value was never set, it will return the default value.
return balance[_addr];
}
}