Skip to content

Instantly share code, notes, and snippets.

@andrejrakic
Last active April 28, 2020 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrejrakic/6f8b516c8c1e1f9ad29367ce40c99b72 to your computer and use it in GitHub Desktop.
Save andrejrakic/6f8b516c8c1e1f9ad29367ce40c99b72 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.6.6+commit.6c089d02.js&optimize=false&gist=
pragma solidity ^0.6.0;
contract Mappings {
// Mappings are data structure like dictionary in C# or hash tables in C++
// O(1)
// mapping(key => value) myMapping;
// One can make nested mappings as well
// Mappings are like databases for blockchain and they are very useful
mapping(uint => string) public names;
mapping(uint => Book) public books;
mapping(address => mapping(uint => Book)) public myBooks;
struct Book {
string title;
string author;
}
constructor() public {
names[1] = "Adam";
names[2] = "Bruce";
names[3] = "Carl";
}
function addBook(uint _id, string memory _title, string memory _author) public {
books[_id] = Book(_title, _author);
}
function addMyBook(uint _id, string memory _title, string memory _author) public {
myBooks[msg.sender][_id] = Book(_title, _author);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment