Sparse arrays

Arrays can be sparse, meaning an array can have a hole in it. Elements can be assigned to index 1 and 2 of an array, leaving 3 and 4 blank and then assigning elements 5 and 6. Any hole in the array will have a default value of nil, as the following code segment demonstrates:

arr = { }

arr[1] = "x"
arr[2] = "y"
-- arr[3] is nil by default
-- arr[4] is nil by default
arr[5] = "z"
arr[6] = "w"

for i=1,6 do
print (tostring(arr[i]))
end

This implies that setting the value of an existing index to nil will introduce a hole in the array. These holes can sometimes cause unexpected issues.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.38.92