Data Structures & Algorithms

YAML Arrays

This article will go through how to utilize the arrays in a YAML file. Since it is widely used when assembling or in need of support variable information, YAML allows the user to record the variables with many values in the array data type.

In YAML, an array may be defined in two ways. Each element is defined on a single line in the standard style, and each line begins with the “-” character. Another frequent style is a single-line format in which all the items appear on a single line separated by commas and surrounded by a bracket.

Example 1: Simple Array in Multiple Lines in a YAML File in Ubuntu 20.04

In this example, we will create a YAML file in which an array with multiple lines is defined. The YAML file is created as a compose file for a docker image file. The file is created in a subdirectory on our desktop and is accessed through our Ubuntu terminal.

On our command terminal, we write the following commands to create a subdirectory on our desktop. In that subdirectory, we create a docker compose file in the “.yml” format.

First, we use the cd command with the Desktop keyword to get to the desktop directory. After getting to the desktop directory, we write the mkdir command with the name of the directory. This creates a new subdirectory on our desktop. Now, we access this subdirectory by using the cd command again. In this subdirectory, we create a YAML file by using the nano command.

As this command is entered, a docker compose file in the YAML format is created in our directory and opened in our terminal where we can edit it and save it, as seen in the following directory flow snippet:

We can open this file in the text editor as well. Ubuntu allows us to edit the file and save it for alterations.

In the previous snippet of our YAML file in the text editor, we can see that the compose file has several variables with different data types. The last variable in this file is the array data type. YAML does not have a restriction on defining the data type as it identifies the data type on its own.

The array format in this example is the variable name followed by the elements defined in the multiple lines with a “-“ character. In this example, we created an array of colors by using the standard format consisting of multiple lines.

Example 2: Simple Array in a Single Line in a YAML File in Ubuntu 20.04

In this example, we create a YAML file in which an array is defined in a single line. The YAML file is created as a compose file for a docker image file. The file is created in a subdirectory on our desktop and is accessed through our Ubuntu terminal.

In our Ubuntu command terminal, we write the following commands to create a subdirectory on our desktop. In that subdirectory, we create a docker compose file in the “.yml” format.

To begin, we use the cd command in conjunction with the Desktop keyword to get to the desktop directory. After we arrive in the desktop directory, we use the mkdir command with the directory name to create a new subdirectory on our desktop.

We create a YAML file in it with the nano command. As soon as we type this command, a docker compose file in YAML format is saved in our new directory as shown in the following:


In the previous snippet of our YAML file in the text editor, we can see that the compose file has several variables with different data types. The last variable in this file is the array data type. YAML does not have a restriction on defining the data type as it identifies the data type on its own. The array format in this example is the variable name followed by the elements defined in one single line separated by commas and enclosed in a square bracket.

Example 3: Nested Array in a YAML File in Ubuntu 20.04

In this example, we build a YAML file with a nested array. The YAML file is produced as a docker image compose file. The file is generated in a subdirectory on our desktop and is accessible using our Ubuntu terminal.

We use the cd command with the Desktop keyword to access the desktop directory. We use the mkdir command with the directory name to establish a new subdirectory on our desktop after we reach the desktop directory.

We use the nano command to build a YAML file in it. As soon as we type this command, a YAML-formatted docker compose file is opened on our terminal in which we can write the code and save it in our new directory as shown in the following:


We can see in the previous excerpt of our YAML file in the text editor that the compose file has numerous variables with different data kinds. This file’s last variable is the array data type. This array is nested because one of its components is another array. The normal format is used in this array. While the last element of this array uses the single line format in which the elements are encased in curly brackets and their values are placed on a single line. This array was hierarchically formed with two formats and saved the various attributes of an automobile.

Conclusion

In this article, we discussed the array data type in the YAML data serialization language. YAML allows the variables to be defined in the array data type as most languages do because they are an efficient approach to saving a huge amount of data on one variable and are easily accessible and traversable. We created the arrays in different formats that YAML allows which are single line and multiple lines formats. We also created a nested array in a YAML file which used both formats while defining the elements.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.