An array is a data structure that is used to store a list of data
So we saw earlier that a variable is a way of storing a single piece of information with a specific name in a computer program. The value one thousand is stored as the variable profit.
profit = 1000
An array is a way of storing a list of data with a specific name in a computer program. Here we have a list of categories of fixed assets “buildings, land, computer, equipment” that we store as the array fixed_assets
fixed_assets = "buildings|land|computer|equipment"
You will need to use a separator to define the different elements within an array. In this example I used a pipe “|” also called the vertical bar to separate the different elements.
We can store any type of data in an array it can be a list of strings like in the first example for fixed_assets or a list of numbers.
used_accounts = "610000|610001|610002|610003"
The data is stored in the array in a sequential manner, so in a specific order. Each element has a specific position in the list. The position of an element in a list is known as its index. You can think of an index like an address - it’s what we use to locate an item in a list. Usually, and this is also the case in liquid, lists start their index at 0 and then add one for each value. Knowing an item’s index allows us to select a value from a list and do something with it in our code.
fixed_assets = "buildings|land|computer|equipment" index 0 1 2 3
Arrays are used when there is a need to use many variables of the same type.
When we start writing more complex code, we’ll start working with more pieces of data. But having lots of data means it can get messy real fast. To keep our data organized, we’ll want to use data structures.
Arrays are a very basic data structure that allow you to store multiple pieces of data that are related to each other together.
Since the data is stored in a specific, linear sequence we can easily access the piece of data that we need.
Updated 5 months ago