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
// you cant directly for or foreach on mappings in solidity
// but if you really need it, you can store an array of indices
mapping(address => uint) public childs;
address[] addressIndices;
function addChild(address _address, uint value) public {
childs[_address] = value;
addressIndices.push(_address);
}
function sumOfChildValues() public returns (uint sum) {
sum = 0;
uint arraylength = addressIndeices.length; // for lower energy cost reasons
for (uint i = 0; i < arraylength; i++) {
sum += childs[addressIndices[i]];
}
}
function removeChild(address _address) public {
delete childs[_address];
uint arraylength = addressIndeices.length; // for lower energy cost reasons
for (uint i = 0; i < arraylength; i++) {
if (addressIndices[i] == _address) {
addressIndices[i] = addressIndices[arraylength - 1];
break;
}
}
addressIndices--;
}
foreach in solidity
xxxxxxxxxx
pragma solidity ^0.5.0;
contract LedgerBalance
{
mapping(address => uint) public balances;
function updateBalance(uint newBalance) public {
balances[msg.sender] = newBalance;
}
}
xxxxxxxxxx
mapping(datatype(KEY) => datatype(VALUE)) <Access Specifier> <MAPPING VARIABLE DECLARATION 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
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.3;
contract Nested {
struct Record {
string id;
uint requestTime;
uint releaseTime;
Direction dir;
}
struct Direction {
bool foo;
uint bar;
}
mapping(address => Record[]) records;
function insert(address key, string memory id, uint requestTime, uint releaseTime, bool foo, uint bar) public {
Direction memory d = Direction({
foo: foo,
bar: bar
});
Record memory r = Record({
id: id,
requestTime: requestTime,
releaseTime: releaseTime,
dir: d
});
records[key].push(r);
}
function inspect(address key) public view returns(Record[] memory) {
return records[key];
}
}
xxxxxxxxxx
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.3;
contract Nested {
struct Record {
string id;
uint requestTime;
uint releaseTime;
Direction dir;
}
struct Direction {
bool foo;
uint bar;
}
mapping(address => Record[]) records;
function insert(address key, string memory id, uint requestTime, uint releaseTime, bool foo, uint bar) public {
Direction memory d = Direction({
foo: foo,
bar: bar
});
Record memory r = Record({
id: id,
requestTime: requestTime,
releaseTime: releaseTime,
dir: d
});
records[key].push(r);
}
function inspect(address key) public view returns(Record[] memory) {
return records[key];
}
}
xxxxxxxxxx
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.3;
contract Nested {
struct Record {
string id;
uint requestTime;
uint releaseTime;
Direction dir;
}
struct Direction {
bool foo;
uint bar;
}
mapping(address => Record[]) records;
function insert(address key, string memory id, uint requestTime, uint releaseTime, bool foo, uint bar) public {
Direction memory d = Direction({
foo: foo,
bar: bar
});
Record memory r = Record({
id: id,
requestTime: requestTime,
releaseTime: releaseTime,
dir: d
});
records[key].push(r);
}
function inspect(address key) public view returns(Record[] memory) {
return records[key];
}
}