Динамические массивы

Массивы и коллекции / Массивы

-- No size to declare: the array grows
-- as items are added to its end
local squares = {}
local n = 1
while n * n < 50 do
  squares[#squares + 1] = n * n
  n = n + 1
end
-- squares = 1, 4, 9, 16, 25, 36, 49
print("squares = " .. table.concat(squares, ", "))

-- An existing index is replaced
squares[1] = 100
-- squares = 100, 4, 9, 16, 25, 36, 49
print("squares = " .. table.concat(squares, ", "))

-- Reading past the end is not an error
local past_end = squares[10]
-- past_end = nil
print("past_end = " .. tostring(past_end))