Table Module
The table module extends Lua’s standard table manipulation functionality, providing additional useful functions for data processing. This module complements Lua’s native table library by adding common operations that are not available in the standard library.
Main features:
-
Extension of Lua’s native
table -
Functions optimized for performance
Available Functions
Section titled “Available Functions”1. table.contains(tabela, valor)
Section titled “1. table.contains(tabela, valor)”Checks whether a specific value is present in a Lua table.
Parameters:
Section titled “Parameters:”-
tabela (table): The table to be checked (can be an array or an associative table)
-
valor (any Lua type): The value to search for in the table
Return:
Section titled “Return:”- boolean:
trueif the value is found in the table,falseotherwise
Behavior:
Section titled “Behavior:”-
Iterates over all key-value pairs of the table using
pairs() -
Compares each value with the searched value using strict equality (
==) -
Returns
trueon the first occurrence found -
Returns
falseif it traverses the entire table without finding the value -
Works with numerically indexed tables (arrays) and associative tables
Usage Example:
Section titled “Usage Example:”-- Check if a value exists in an arraylocal frutas = {"maçã", "banana", "laranja", "uva"}local tem_banana = table.contains(frutas, "banana")print("Tem banana?", tem_banana) -- true
local tem_morango = table.contains(frutas, "morango")print("Tem morango?", tem_morango) -- false
-- Check in associative tablelocal configuracoes = { timeout = 30, retries = 3, debug = false, hostname = "servidor.local"}
local tem_timeout = table.contains(configuracoes, 30)print("Tem valor 30?", tem_timeout) -- true
local tem_true = table.contains(configuracoes, true)print("Tem valor true?", tem_true) -- false
-- Check complex typeslocal usuarios = { {id = 1, nome = "Alice", ativo = true}, {id = 2, nome = "Bob", ativo = false}, {id = 3, nome = "Carol", ativo = true}}
-- For complex types, it must be the same referencelocal usuario_bob = usuarios[2]local encontrou_bob = table.contains(usuarios, usuario_bob)print("Encontrou Bob?", encontrou_bob) -- true
-- This search will return false because it's a new objectlocal encontrou_novo_bob = table.contains(usuarios, {id = 2, nome = "Bob", ativo = false})print("Encontrou novo Bob?", encontrou_novo_bob) -- false2. table.slice(tabela, primeiro, último)
Section titled “2. table.slice(tabela, primeiro, último)”Extracts a slice (subarray) from a numerically indexed table (array).
Parameters:
Section titled “Parameters:”-
tabela (table): The Lua array from which to extract the slice
-
primeiro (number, optional): Starting index of the slice (default: 1)
-
último (number, optional): Ending index of the slice (default: length of the table)
Return:
Section titled “Return:”- table: New array containing the elements of the specified slice
Behavior:
Section titled “Behavior:”-
Creates a new table containing the elements from index
primeirotoúltimo(inclusive) -
If
primeirois omitted ornil, starts from the first element (index 1) -
If
últimois omitted ornil, goes to the last element of the table -
If
primeirois greater thanúltimo, the function still iterates but values will benilfor nonexistent indices -
Does not perform bounds checking - indices outside the table range will result in
nilvalues -
Preserves the original order of elements
Usage Example:
Section titled “Usage Example:”-- Extract part of an arraylocal numeros = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}
-- Slice from index 3 to 7local fatia1 = table.slice(numeros, 3, 7)-- fatia1 = {30, 40, 50, 60, 70}
-- Slice from the start to index 5local fatia2 = table.slice(numeros, nil, 5)-- fatia2 = {10, 20, 30, 40, 50}
-- Slice from index 8 to the endlocal fatia3 = table.slice(numeros, 8)-- fatia3 = {80, 90, 100}
-- Slice with indices out of boundslocal fatia4 = table.slice(numeros, -2, 15)-- fatia4 = {nil, nil, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, nil, nil, nil, nil, nil} (does not handle bounds)
-- Slice with first > lastlocal fatia5 = table.slice(numeros, 5, 3)-- fatia5 = {50, 40, 30} (iterates in descending order)
-- Batch data processinglocal function processar_em_lotes(dados, tamanho_lote) local lotes = {} local total = #dados
for i = 1, total, tamanho_lote do local fim = math.min(i + tamanho_lote - 1, total) local lote = table.slice(dados, i, fim) table.insert(lotes, lote) end
return lotesend
local dados_grandes = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}local lotes = processar_em_lotes(dados_grandes, 5)-- lotes = {{1,2,3,4,5}, {6,7,8,9,10}, {11,12,13,14,15}}