My Image
CoursesQuizzesProblemsContestsSmartBooks
Contest!

No results found

LOGINREGISTER
My ProgressCoursesQuizzesProblemsContestsSmartbooks
Published on 13 Sep 2021
Multidimensional arrays in solidity
array operations in solidity
img
Aniket Savji
0
Like
1611

Introduction

Ususally we use struct and other data types to store multiple properties. But calling those for larger iteration is very difficult and ruins quality of our dapps. 2d arrays can help us solve those problems. Note that for multidimentional arrays data type remains constant  unlike struct.

2D Arrays 

We will take a look at 2d arrays in this smartbook. Smart contract we will be building will store different key, value pairs for different user public addresses

Declaration

We will map our 2d array with address, it will store different key value pairs for a user.

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

contract key_val{
    
    mapping(address => string[][]) private key_val_pairs;
    
}

Initialisation 

Let's create a function which will take those key value pairs from user and save it in our 2d array.

function save(string memory _key, string memory _value) public{
        key_val_pairs[msg.sender].push([_key, _value]);
}

Everytime user saves key values , array will store a key value paired array thus we can retrieve all the saved pairs at once.

Getter function 

function get_pairs() public view returns(string [][] memory){
        return key_val_pairs[msg.sender];
}

Suppose if we would have saved these pairs in an struct , we would have needed to call this function for each pair stored in the stuct. But this way all the pairs can be called at onve. It have some limitations but most of the cases it is useful to tackle some simple problems.

Complete Code

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

contract key_val{
    
mapping(address => string[][]) private key_val_pairs;
   
 function save(string memory _key, string memory _value) public{
        key_val_pairs[msg.sender].push([_key, _value]);
  }

function get_pairs() public view returns(string [][] memory){
        return key_val_pairs[msg.sender];
  }
}
Enjoyed the SmartBook?
Like
logo
contact@dapp-world.com
Katraj, Pune, Maharashtra, India - 411048

Follow Us

linkedintwitteryoutubediscordinstagram

Products

  • SmartBooks
  • Courses
  • Quizzes
  • Assessments

Support

  • Contact Us
  • FAQ
  • Privacy Policy
  • T&C

Backed By

ah! ventures

Copyright 2023 - All Rights Reserved.

Recommended from DAppWorld
img
1 May 2021
How to connect Ganache with Metamask and deploy Smart contracts on remix without
Set up your development environment with (Metamask + Ganache + Remix) and skip truffle :)
3 min read
11509
5
img
8 Jul 2021
How to interact with smart contarct from backend node js
call and send functions from backend server side using nodejs
3 min read
8103
2
img
18 Aug 2021
Send transaction with web3 using python
Introduction to web3.py and sending transaction on testnet
3 min read
6229
5
img
5 Aug 2021
Deploy Smart Contract on Polygon POS using Hardhat
how to deploy smart contracts on polygon pos chain using hardhat both mainnet and testnet ?
3 min read
5540
3