envoy1084
/
30-Days-of-Solidity
30 Days of Solidity step-by-step guide to learn Smart Contract Development.
WARNING: This repository is currently undergoing updates and revisions to incorporate the latest information and advancements in Solidity programming. Please be advised that the content may not be up-to-date or accurate during this time. We expect the updates to be completed within the next 30 days, and appreciate your patience during this process. Thank you for your understanding.
Contents
- Day 1 - Licenses and Pragma
- Day 2 - Comments
- Day 3 - Initializing Basic Contract
- Day 4 - Variables and Scopes
- Day 5 - Operators
- Day 6 - Types
- Day 7 - Functions
- Day 8 - Loops
- Day 9 - Decision Making
- Day 10 - Arrays
- Day 11 - Array Operations
- Day 12 - Enums
- Day 13 - Structs
- Day 14 - Mappings
- Day 15 - Units
- Day 16 - Require Statement
- Day 17 - Assert Statement
- Day 18 - Revert Statement
- Day 19 - Function Modifiers
- Day 20…
This is Day 14 of 30 in Solidity Series
Today I Learned About Mappings in Solidity.
Mapping in Solidity acts like a hash table or dictionary in any other language. These are used to store the data in the form of key-value pairs, a key can be any of the built-in data types but reference types are not allowed while the value can be of any type. Mappings are mostly used to associate the unique Ethereum address with the associated value type.
Syntax:
mapping(key => value) <access specifier> <name>;
Example:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
contract Mappings {
struct Student {
string name;
string subject;
uint marks;
}
mapping(address => Student) public addressToStudent;
Student student1;
function addDetails() public {
student1 = Student("Daniel","Maths",85);
addressToStudent[0x5B38Da6a701c568545dCfcB03FcB875f56beddC4] = student1;
}
}
Here we map a address to a Student struct.and the function addDetails adds the details of the student to struct student1 and then maps it to address 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4.
Output when we pass the above address in addressToStudent mapping:
{
"0": "string: name Daniel",
"1": "string: subject Maths",
"2": "uint256: marks 85"
}
Top comments (0)