What are crypto tokens and coins based on? [Mapping and Array]

This will be a more in-depth article for those who are interested in Smart Contracts and coding. We’ll explain the usage of arrays and mappings in practice.

Arrays allow you to store values of the same type in an orderly manner. This is useful when you need to work with data related to a common feature. In Solidity, they can be divided into 2 types: ordinary index arrays, and mapping (as hash tables). Here I present you a few simple examples that show how they work. In these examples I used string type.

First of all let’s talk about string. String literals are written with either double or single-quotes (“foo” or ‘bar’). They do not imply trailing zeroes as in C; “foo” represents three bytes(not four). As with integer literals, their type can vary, but they are implicitly convertible into bytes1, …, bytes32, if they fit, into bytes and into string.

Arrays

Arrays can have a compile-time fixed size or they can be dynamic. For storage arrays the element type can be arbitrary. An array of fixed k size and element T type is written as T[k], an array of dynamic is written as T[]. Arrays have .push (adds an element to the end of the array only for the dynamic array) and .length (gets count of elements) members.

Here is an example of the contract “Lines”.

pragma solidity >=0.4.0 <0.6.0;


contract Lines {
    // Dynamic array of strings
    string[] lines;
    
    // Get length of array
    function getLinesCount()public constant returns (uint) {
        return lines.length;
    }
    
    // Add string to array
    function addLine(string s) public {
        lines.push(s);
    }
    
    // Get last string
    function getLastLine() public constant returns (string) {
        return lines[lines.length-1];
    }
    
    // Get string by index 
    function getLineByIndex(uint index) public constant returns (string) {
        if(index >= 0 && index < lines.length) {
            return lines[index];
        } else {
            return "empty";
        }
    }
    
    // Delete string by index 
    function deleteLineByIndex(uint index) public {
        require(index < lines.length);
        delete lines[index];
    }
}

Now we will talk about this in detail.

addLine – we can use .push because it is a dynamic array. If we have an index array like string[5] lines we can`t use .push. We will use an index to add new string;

getLastLine – it gets last element of array. We should write length – 1 because counting elements start from zero;

getLineByIndex – it gets element by index. We need only to check index and return string; in other cases, it will return “empty”;

deleteLineByIndex – it deletes element by index. This function is very interesting because if you have array.length = 8 and you want to delete the 5th element, you use delete array[4]and array.length will still be 8. You just clear the 5th element and if you try to get it, you will get an empty string or zero (it depends on array type). But if you want to delete an element you have to go through the array.

Mappings

Mapping isn`t an index array and doesn`t have members. You declare mapping types with the syntax mapping(_KeyType => _ValueType). The _KeyType can be any elementary type. This means it can be any of the built-in value types plus bytes and string. User-defined or complex types like contract types, enums, mappings, structs and any array type apart from bytes and string are not allowed. _ValueType can be any type, including mappings.

Here is an example of the contract “Nicknames” which stores uint8 (I wrote about int in the previous article) values by nickname. 

pragma solidity >=0.4.0 <0.6.0;


contract Nicknames {
    // Mapping key - string, value - uint8
    mapping(string => uint8) values;
    
    // Add value to mapping
    function addValue(string nick, uint8 val) public {
        values[nick] = val;
    }
    
    // Get value by nick
    function getValue(string nick) public constant returns (uint8) {
        return values[nick];
    }
    
    // Delete value by nick
    function delValue(string nick) public {
        delete values[nick];
    }
}

Now we will talk about this in detail. 

addValue – it assigns value to mapping by key; if key matches value, it will be redefined;

getValue – here we can get value by key; if key doesn`t exist, function will return zero;

delValue – this function deletes an element, that is different than with array; if you try to get value by deleted key, it will return zero.

What are the benefits of mapping?

During the transaction the Smart Contract receives state data of a block, including address of the sender of the transaction. If you specify a key mapping of the address type, we can associate the sender of the transaction with some data, for example, with a balance. Put simply, we can assign ownership of specific currencies and store their balance.

This is exactly the principle of how currencies in the blockchain are created.