util.table.len

Shared

Retrieves the length of a table


Syntax

local length = util.table.len(
    input
)

Parameters

TypeNameDescription
tableinputTable to measure

Returns

TypeNameDescription
intlengthLength of the table

Examples

Get the length of a regular table
local t = {1, 2, 3, 4, 5}
local len = util.table.len(t)

core.engine.print("info", len) -- 5
Get the length of a packed table using n field
local packed = util.table.pack(1, nil, 3, nil, 5)
local len = util.table.len(packed)

core.engine.print("info", len) -- 5
Length of a table with holes may vary
local t = {1, 2, nil, 4}
local len = util.table.len(t)

core.engine.print("info", len) -- 4 (may vary, depends on #)
Get the length of an empty table
local t = {}
local len = util.table.len(t)

core.engine.print("info", len) -- 0
Compare util.table.len with the # operator
local t = {1, 2, 3}

core.engine.print("info", util.table.len(t)) -- 3
core.engine.print("info", #t) -- 3
util.table.len is reliable with nil holes
local args = util.table.pack("a", nil, "b", nil, "c")

core.engine.print("info", #args) -- May be incorrect with nil holes
core.engine.print("info", util.table.len(args)) -- 5 (always correct)
Use util.table.len for safe iteration
local t = util.table.pack(1, nil, 2, nil, 3)

for i = 1, util.table.len(t) do
    core.engine.print("info", i, t[i])
end
Check if a table is empty
local t = {}
if util.table.len(t) == 0 then
    core.engine.print("info", "Table is empty")
end
util.table.len only counts the array part
local t = {10, 20, 30, key = "value"}
local len = util.table.len(t)

core.engine.print("info", len) -- 3 (only counts array part)

On this page