Iterating over an array with index

Arrays and collections / Arrays

local numbers = {2, 3, 5, 7, 11, 13, 17}

-- ipairs gives the index and the item
local text = ""
for index, number in ipairs(numbers) do
  if index > 1 then
    text = text .. "; "
  end
  text = text .. number
end
-- text = 2; 3; 5; 7; 11; 13; 17

-- The index lets you change the item in place
for index = 1, #numbers do
  numbers[index] = numbers[index] * 10
end
-- numbers = 20, 30, 50, 70, 110, 130, 170

print("text = " .. text)
print("numbers = " .. table.concat(numbers, ", "))