Just as you are able to find the length of a string with the # operator, you can find the length of an array as well. This is because the # operator gives back the size of a table. We can use this operator to loop through an array that has a dynamic size, for example, such as the following code snippet:
arr = { "a", "b", "c", "d", "e", "f", "g" }length = #arr print ("array length: " .. length)for i=1,#arr do print (arr[i])end
The length operator # will only count array elements starting from index 1. This means if you use index 0, it will not be counted towards the number of elements in the array:
arr = { }arr[0] = "x" -- not counted towards lengtharr[1] = "y"arr[2] = "z"length = #arr -- length = 2!print ("array length: ...