lua array is empty
Declare an empty array in Lua using square brackets:
myArray = {}
Access the length of the array using the
#
operator:length = #myArray
Print the length of the array to the console:
print(length)
Iterate over the array using a loop:
lua for i = 1, #myArray do -- Code to execute for each element in the array end
Add an element to the array using the
table.insert()
function:table.insert(myArray, "element")
Access an element in the array by its index:
element = myArray[1]
Remove an element from the array using the
table.remove()
function:table.remove(myArray, 1)
Check if the array is empty using the length comparison:
lua if #myArray == 0 then -- Code to execute if the array is empty else -- Code to execute if the array is not empty end
Clear the array by setting it to an empty table:
myArray = {}