Ethereum: Populate mapping in global scope returns error


Here is an article about Ethereum mapping:

Ethereum maps: a simple example

Wen he together with Ethereum, he of the finds the contact of mapping. A mapping is a Data job that allows you to something values ​​associated with specific Keys (also known as “keys” or “indices” or “indices”) in. In this article, we will explore How to create and use a simple mapping in the global.

What is a mapping?

In Ethereum, a mapping is essentially a wey storing sets of values ​​that areexed by ther matrix in. The Key walue pairs are stored in a singles (the “key” field) followed by an index value in index value in another field (the “index” This allows efficient data search and recovery.

Creation of a simple mapping

Here is a simple example of creating a mapping two elements:

`SOLIDITY

Mymapping contract {

uint8 [2] public mymap;

}


In this example, we define a contraction calledMymappinge matrix of 2 elments ( uint8) called 'Mymap.


Popular mapping

To the same the allocation with Data, you can through functions, that will come and value as arguments. We belive two functions:

`SOLIDITY

Mymapping contract {

Functions set (Uint8 index, Uint8 value) a public {

Mymap [index] = value;

}

GET FUNCTION (Uint8 Index) Public View returns (Uint8) {

Return Mymap [index];

}

}

In theset, we are the value provid index. In theett ‘function, we simply return the value associated with the given index.

Using mapping

Now that we have a created and populated from assignment, let’s it to so soome data:

`SOLIDITY

Mymapping contract {

Mymapping Mymap;

Builder () apublic {

Mymap.set (0, 1); // Establish the first index 0

Mymap.set (1, 2); // Establish the second index 1

}

Function Getmyitem (Uint8 Index) Public View returns (Uint8) {

Return Mymap.get (index);

}

}


In this example, we create aMymapingcontraction and establish two elements in the lloction it 'set function. That is the Get to recover an element of the allocation.


Example use





Ethereum: Populate mapping in global scope returns error

To try your mapping, let's call the Getmyitem:

`SOLIDITY

Pragma solidity ^0.8.0;

Mycontractor contract {

Mymapping Mymap;

Builder () apublic {

Mymap.set (1, 5); // Establish the element index 1 to

}

Getmyitem Function (Uint8 Index) Public View returns (Uint8) {

Return Mymap.get (index);

}

}

In this example, we call the Getmyitem As there is no element in index 0 inour assignment, the function simply returns 0.

Conclusion*

Ethereum assigns provide a powerful way to something and recover data efficently. In this article, we explore How to crate a simple assignment in the global use solidity. That’s what mapping with Data and use it to something and recover elements. This example demonstrated the versatility of Ethereum’s assignments and ther possiable applications in the development of intracts.

I hope it helps you to better understand the mapping! Get me if you have more questtions or need additional examples.


Leave a Reply

Your email address will not be published. Required fields are marked *