DEV Community

Cover image for Solidity for Beginners - Data Structures in Solidity
Vinit Gupta
Vinit Gupta

Posted on

Solidity for Beginners - Data Structures in Solidity

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

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.

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.

// 2 ways to create arrays
uint [3] numbers = [1,2,3];
string [] users;
Enter fullscreen mode Exit fullscreen mode

Mappings

Maps are almost in every language known to the Homo Developerians.
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 :

// THIS IS JAVASCRIPT CODE NOT SOLIDITY
books = {
  "The Silent Patient" : {
     author : "Alex Michaelidis",
     released : "5 Feb, 2019",
     isbn : "9781250301697"
  },
....
};

Enter fullscreen mode Exit fullscreen mode

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?

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

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.

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.

// creating enum
enum DayOfWeek{ MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY };
// assigning value
DayOfWeek today = DayOfWeek.WEDNESDAY;
Enter fullscreen mode Exit fullscreen mode

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??
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:

enum Subject( COMPUTER, SCIENCE, MATHS, ENGLISH);
Enter fullscreen mode Exit fullscreen mode

Now, we can create our struct :

enum Subject( COMPUTER, SCIENCE, MATHS, ENGLISH);

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

mapping(address => Student) students;
Enter fullscreen mode Exit fullscreen mode

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 🐥 and Github 🐙

Top comments (0)