Solidity Steps
  • Learning Solidity
  • Step 1
    • 1: Introduction
    • 2: Data Types
    • 3: Functions
    • 4: Control Structures
    • 5: State Variable
    • 6: Local Variables
    • 7: Global Variables
    • 8: View Keyword
    • 9: Pure Keyword
  • STEP 2
    • 10: Immunable Keyword
    • 11: Events
    • 12: Condition
    • 13: While Loop
    • 14: Do While Loop
    • 15: For Loop
    • 16: Required
    • 17: Assert
    • 18: Revert
    • 19: Modifier
  • STEP 3
    • 20: Constructor
    • 21: Mapping
    • 22: Array
    • 23: Enum
    • 24: Structs
    • 25: Data Location
    • 26: Inheritance
    • 27: The Shadowing Effect
    • 28: Super Keyword
    • 29: Visibility
  • STEP 4
    • 30: Interface
    • 31: Abstract Contract
    • 32: Payable
    • 33: Using type()
    • 34: Sending Ether
    • 35: Receive
    • 36: Fallback
    • 37: Call
    • 38: DelegateCall
    • 39: Calling Other Contracts
  • STEP 5
    • 40: Factory Contract
    • 41: Proxy Contract
    • 42: Create2
    • 43: Try and Catch
    • 44: Solidity Library
    • 45: ABI Encoded
    • 46: ABI Decoded
    • 47: Keccak256
    • 48: Function Signature Hash
  • TIPS
    • Tips: Solidity by "Immunable"
    • Tips: Truffle Tutorial
    • Tips: Microblog Dapp
    • Tips: Reentrancy
    • Tips: Slither Tutorial
    • Tips: Remix Tutorial
    • Tips: Hardhat Tutorial
  • CAREER
    • 💲Cover Letter
    • 💲Resume
  • ABOUT
    • Contact me
Powered by GitBook
On this page
  • Mapping
  • Dynamic Mapping
  1. STEP 3

21: Mapping

Mapping

Mapping is a data structure in Solidity that allows a programmer to store, retrieve and modify data in a key-value pair format. It is similar to a hash table or dictionary in other languages.

  • Example 1 Let's assume we want to store a list of people and their ages. We can do this with a mapping like this:

mapping (address => uint) public peopleAges;
  • Example 2 Now, let's assume we want to store a list of people and their names. We can do this with a mapping like this:

mapping (address => string) public peopleNames;
  • Example 3 Finally, let's assume we want to store a list of people and their ages and names. We can do this with a mapping like this:

mapping (address => (uint, string)) public peopleAgesNames;

Dynamic Mapping

Dynamic mapping is a data structure in Solidity that allows for variable keys and values. It is used to store data in a key-value format, as in a dictionary.

  • Example 1 Consider the following example:

mapping(address => uint) public balances;

This creates a mapping from an address to a uint (unsigned integer). This means that each address in the blockchain can have its own integer value. This is useful for tracking balances or other values associated with each address.

  • Example 2 Here is another example:

mapping(uint => string) public messages;

This creates a mapping from a uint (unsigned integer) to a string. This allows us to store a string value associated with a given integer. This could be used to track messages associated with a particular event or transaction.

  • Example 3 Finally, here is an example using multiple data types:

mapping(address => mapping(uint => string)) public userData;

This creates a mapping from an address to a mapping of uints to strings. This allows us to store a string value associated with a given address and a given uint. This could be used to store user data associated with a particular address and a particular event or transaction.

That's it for the lesson 21! In the next lesson, Array

Previous20: ConstructorNext22: Array

Last updated 1 year ago