Syntax
for index, tag in ipairs(tags) do
print(index, tag)
endExamples
Walk a sequence in order
The loop prints 1 HTML, 2 CSS and 3 Lua. ipairs supplies the index as well as the value.
local tags = { "HTML", "CSS", "Lua" }
for index, tag in ipairs(tags) do
print(index, tag)
endA gap stops iteration
Only 1 HTML is printed. Key 2 is missing, so ipairs never reaches key 3. A numeric loop with explicit bounds lets you handle gaps deliberately.
local tags = { [1] = "HTML", [3] = "Lua" }
for index, tag in ipairs(tags) do
print(index, tag)
end
for index = 1, 3 do
print(index, tags[index] or "(missing)")
endBest practices
- Keep sequence keys consecutive from 1 when using ipairs. A false value is visited; nil stops iteration.
- Use pairs for named keys, but do not rely on its iteration order.
- Do not use #table as a reliable item count for tables with gaps. Supply explicit bounds when gaps are meaningful.
Runtime & versions
Lua interpreter or a host application. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
Lua reference manuals