Solidity for Beginners - Data Structures in Solidity
Tags: web3, blockchain, codenewbie
Published: 2022-11-09 09:09:02

I am back with another post. This time it will be about data structures in Solidity.

<img src="https://media.giphy.com/media/3o6ZtkAstZU7sTvXTa/giphy.gif" alt="solidity meme"/>

Solidity has Data Structures too? Yes kid it's true.

Now, what are the data structures that Solidity supports?

- Arrays
- Mappings
- Enums
- Structs

If you are familiar with C++, you are basically familiar with all these. But since it is a beginners blog post, we will have to go through each one of them.
<img src="https://media.tenor.com/BiiTsQ_0j7AAAAAC/mendokusai-what-a-drag.gif"/>

Well then, let's get to it.

## Arrays
If you have done programming even a little bit, you probably already know what arrays are.

They are simply a structure that provides us the functionality of storing similar type of data in a single variable. Basically, like a list of things in real life.

To create an array in Solidity, you first to know the type of data you want to store. Data types can be int, uint, string, boolean, addresses and enums.

You can create an empty array and then append the items or you can just initialize the elements along with the size of the array.

```solidity
// 2 ways to create arrays
uint [3] numbers = [1,2,3];
string [] users;
```

## Mappings
Maps are almost in every language known to the `Homo Developerians`.
<img src="https://media.giphy.com/media/o37e1VlwzOvFS/giphy.gif" alt="solidity meme"/>

Just kiding, we are just Homo Sapiens with a great toy(Computers).

Well, back to maps. These are just a way of storing data by linking it with another data.
Basically, a `key -> value` mapping.

Let's see an examples : 
```javascript
// THIS IS JAVASCRIPT CODE NOT SOLIDITY
books = {
  "The Silent Patient" : {
     author : "Alex Michaelidis",
     released : "5 Feb, 2019",
     isbn : "9781250301697"
  },
....
};

```

Here, books is a map, as well as each book inside it.
Important thing to note here is that the key and the value can be of any type.

But, how do we create a map in Solidity?

```solidity
// creating a map with key as address data type and value as string
mapping (
    address => string) students;
// adding data
students[0xDEE7796E89C82C36BAdd1375076f39D69FafE252] = "Peter Parker";
```

## Enums
As an alien race, we developers had to create something different. So we created enums.

> A data type that can have only certain specified values.

<img src="https://media.giphy.com/media/SACoDGYTvVNhZYNb5a/giphy.gif" alt="soldity meme"/>

Didn't get it? Let's put it simply.
You know how days of week can only have these fixed names.
You cannot just go on and add a day with your own name to it.

Here enums work the magic. Create a data type `DayOfWeek` that can only have 7 string values specified.

```solidity
// creating enum
enum DayOfWeek{ MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY };
// assigning value
DayOfWeek today = DayOfWeek.WEDNESDAY;
```

I believe you understand enums now. Let's see a kind of extension of enums

## Structs
Structs are used to basically create even complex data types. Structs consist of other data types to be used together in a single data type.

For example, we want to have a data type : `Student`

What data does a student have? 
- Name
- Age
- Email
- Phone Number
- Subjects

But what, you see an Enum data type in the list above??
<img src="https://media.giphy.com/media/jJA5fhjTuIOqc/giphy.gif" alt="solidity meme"/>

Well, if you though Subjects, the you are absolutely correct!!
Let's say this school only teaches 4 subjects and a student can take only 2 of them: 
```solidity
enum Subject( COMPUTER, SCIENCE, MATHS, ENGLISH);
```

Now, we can create our struct : 
```solidity
enum Subject( COMPUTER, SCIENCE, MATHS, ENGLISH);

struct Student{
      string name;
      uint age;
      string email;
      uint phoneNumber
      Subject [] subjects;
}

mapping(address => Student) students;
```

You can see how we are able create complex structures step by step.

If you liked this article, be sure to follow for more such upcoming articles on Solidity and Web3.

You can also follow me on [Twitter 🐥](https://twitter.com/thevinitguptaa) and [Github 🐙](https://github.com/thevinitguptaa)