-
Notifications
You must be signed in to change notification settings - Fork 2
/
015-mappings-in-solidity.sol
38 lines (32 loc) · 1.07 KB
/
015-mappings-in-solidity.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* Mapping in solidity
*
* Mapping is a reference type as arrays and structs.
* Mapping allows one to store data in key value pairs
* for easier retrieval later on the the course of the program
*
* In solidity you can't iterate through a map - you need to store
* the keys in an array and you can't give them sizes
*/
contract LearnMapping {
// syntax to declare a mapping type
// address could be of any daatype
mapping(address => uint256) public myMap;
// create a specific address
// NOTE: Keys in a map have to be unique
function setAddress(address _addr, uint256 _i) public {
myMap[_addr] = _i;
}
// get data stores in a certain key - addr
function getAddress(address _addr) public view returns (uint256) {
return myMap[_addr];
}
// remove an address
function removeAddress(address _addr) public {
// replaces the value of the corresponding key to zero
// as when we were working with arrays
delete myMap[_addr];
}
}