coloX Table Functions
coloX provides convenient table utilities that simplify common Lua array operations.
API Reference
add(tbl, val [, index])
Adds an element to a table.
Parameters:
| Parameter | Type | Description |
|---|---|---|
tbl | table | Target array |
val | any | Value to add |
index | number | Insertion position; append to the end if omitted |
local t = {1, 2, 3}
add(t, 4) -- t = {1, 2, 3, 4}
add(t, 99, 2) -- t = {1, 99, 2, 3, 4}
del(tbl, val)
Finds and removes the first element equal to val.
local t = {1, 2, 3, 2}
del(t, 2) -- t = {1, 3, 2} (remove only the first match)
deli(tbl [, index])
Removes the element at the specified position. Omitting index removes the last element.
local t = {10, 20, 30, 40}
deli(t, 2) -- t = {10, 30, 40} (remove the second item)
deli(t) -- t = {10, 30} (remove the last item)
count(tbl) → number
Returns the number of non-nil elements in the array part of the table (1..#tbl). For tables with holes, the result may be less than #tbl.
local t = {1, 2, 3, 2, 2}
count(t) -- → 5
count({1, nil, 3}) -- → 2
all(tbl) → iterator
Returns an array iterator for for loops, skipping nil elements.
local t = {1, nil, 3, nil, 5}
for v in all(t) do
print(v) -- Prints 1, 3, 5
end
foreach(tbl, func)
Calls func for each non-nil element in the array.
local t = {10, 20, 30}
foreach(t, function(v)
print(v)
end)
-- Equivalent to
foreach({10, 20, 30}, print)
API Index
| Function | Description |
|---|---|
add(tbl, val [, index]) | Add an element |
del(tbl, val) | Remove the first matching value |
deli(tbl [, index]) | Remove by position |
count(tbl) | Count non-nil elements |
all(tbl) | Iterator (skips nil) |
foreach(tbl, func) | Iterate and execute |