Array is an important data type in many programming language. In Lua, arrays are represented by tables. You can imagine this data structure like a warehouse, which can be used to store data type like lists, dictionaries, and other collections of data. Just noted that Arrays in Lua are indexed starting from 1, unlike many other programming languages where indexing starts at 0.
In this tutorial, I will show you how to create, manipulate, and use arrays in Lua.
How to create an array?
In Lua, you can simply define an array with the “local” keyword. All the values are stored inside one by one. Let’s say for example, we create an array for storing the name of the fruits. The syntax is like below:
local fruits = {"Orange", "Mango", "Grape", "Pineapple", "Blueberry"}
So fruits
is an array containing five elements: “Orange”, “Mango”, “Grape”, “Pineapple”, “Blueberry”. Next you can learn how to access the array elements in Lua.
Example of access the Array
As mentioned, Lua array start from index = 1. You can access elements in an array like below:
local fruits = {"Orange", "Mango", "Grape", "Pineapple", "Blueberry"}
print(fruits[1]) -- Output: Orange
print(fruits[2]) -- Output: Mango
print(fruits[3]) -- Output: Grape
print(fruits[4]) -- Output: Pineapple
print(fruits[5]) -- Output: Blueberry
Once you know the position of the element, you can easily call it out.
Can you modify the array elements?
The answer is simply YES. You can easily modify the elements in an array by assigning new values to specific indices. For example, now I would like to modify the second element in the fruit array. Update it from Mango to Watermelon. What you need to do is call it out and replace the value like below.
local fruits = {"Orange", "Mango", "Grape", "Pineapple", "Blueberry"}
fruits[2] = "Watermelon"
print(fruits[2]) -- Output: Watermelon
How to add Elements to Lua Array?
You can surely add additional elements to an array using the table.insert
function. Moreover, you can assign a new value to the next available index. Let me show you in below example:
local fruits = {"Orange", "Mango", "Grape", "Pineapple", "Blueberry"}
-- Using table.insert
table.insert(fruits, "Cherry")
print(fruits[6]) -- Output: Cherry
-- Direct assignment
fruits[7] = "Banana"
print(fruits[5]) -- Output: Banana
In the above example, I show you two possible ways to add new elements. The first way I used is the insert() function. It add the new element at the end of the queue. The second way is by direct assignment. You can assign any values to any specific index.
How to remove elements in Lua Array?
The direct and easy way to remove elements from an array using the table.remove
function. You can see in the below example, after you successfully remove the element in the second position, the current index 2 element will become Grape instead of Mango.
local fruits = {"Orange", "Mango", "Grape", "Pineapple", "Blueberry"}
table.remove(fruits, 2) -- Removes the element at index 2 (Mango)
print(fruits[2]) -- Output: Grape
How to using for loop with Lua Array?
Sometime, you want to print all the values in the array. Instead of calling it out one by one, you can iterate over the elements of an array using a for
loop. If you forgot how to use for loop, you can check out the looping tutorial here.
local fruits = {"Orange", "Mango", "Grape", "Pineapple", "Blueberry"}
for i = 1, #fruits do
print(fruits[i])
end
Noted that the #
operator returns the length of the array. So by using the for loop, the script will print out all the elements inside the fruits array.
Summary of commonly used Array Operations
Below I tidy up all the useful array operations with examples. Hope you can make good use of them and work well with the array operations.
-- Creating an array
local numbers = {10, 20, 30, 40, 50}
-- Accessing elements
print("First element: " .. numbers[1]) -- Output: 10
-- Modifying elements
numbers[3] = 35
print("Modified third element: " .. numbers[3]) -- Output: 35
-- Adding elements
table.insert(numbers, 60)
print("Added element: " .. numbers[6]) -- Output: 60
-- Removing elements
table.remove(numbers, 2)
print("After removal, second element: " .. numbers[2]) -- Output: 35
-- Iterating over the array
print("All elements:")
for i = 1, #numbers do
print(numbers[i])
end
Conclusion
Lua arrays are flexible and powerful. You can store and manipulate collections of data easily. By understanding how to create, access, modify, add, remove, and iterate over arrays, you can effectively manage data in your Lua programs. In the future, I will show you more real cases on how to create a Lua program with the help of the array. Stay tune!