Ir al contenido

Diferencia entre revisiones de «Módulo:Tablas»

Sin resumen de edición
m Agregando más índices a la función z.elemento() para permitir llamadas con más índices
Línea 108: Línea 108:
end
end


function z.elemento(tabla, indice1, indice2, indice3, indice4, indice5, indice6, indice7)
function z.elemento(tabla, indice1, indice2, indice3, indice4, indice5, indice6, indice7, indice8, indice9, indice10, indice11, indice12)
local resultado
local resultado
Línea 114: Línea 114:
return
return
end
end
resultado = tabla[indice1]
resultado = tabla[indice1]
Línea 120: Línea 119:
return resultado
return resultado
end
end
resultado = resultado[indice2]
resultado = resultado[indice2]


Línea 126: Línea 124:
return resultado
return resultado
end
end
resultado = resultado[indice3]
resultado = resultado[indice3]
Línea 132: Línea 129:
return resultado
return resultado
end
end
resultado = resultado[indice4]
resultado = resultado[indice4]
Línea 138: Línea 134:
return resultado
return resultado
end
end
resultado = resultado[indice5]
resultado = resultado[indice5]
Línea 144: Línea 139:
return resultado
return resultado
end
end
resultado = resultado[indice6]
resultado = resultado[indice6]
Línea 150: Línea 144:
return resultado
return resultado
end
end
resultado = resultado[indice7]
if not indice8 or not resultado then
return resultado
end
resultado = resultado[indice8]
if not indice9 or not resultado then
return resultado
end
resultado = resultado[indice9]
if not indice10 or not resultado then
return resultado
end
resultado = resultado[indice10]
resultado = resultado[indice7]
if not indice11 or not resultado then
return resultado
end
resultado = resultado[indice11]
if not indice12 or not resultado then
return resultado
end
resultado = resultado[indice12]
return resultado
return resultado
end
end

Revisión del 00:12 12 nov 2020

La documentación para este módulo puede ser creada en Módulo:Tablas/doc

local z = {}

-- Código copiado de http://lua-users.org/wiki/SortedIteration
function __genOrderedIndex( t )
    local orderedIndex = {}
    for key in pairs(t) do
        table.insert( orderedIndex, key )
    end
    table.sort( orderedIndex )
    return orderedIndex
end

function orderedNext(t, state)
    -- Equivalent of the next function, but returns the keys in the alphabetic
    -- order. We use a temporary ordered key table that is stored in the
    -- table being iterated.

    local key = nil
    --print("orderedNext: state = "..tostring(state) )
    if state == nil then
        -- the first time, generate the index
        t.__orderedIndex = __genOrderedIndex( t )
        key = t.__orderedIndex[1]
    else
        -- fetch the next value
        for i = 1,table.getn(t.__orderedIndex) do
            if t.__orderedIndex[i] == state then
                key = t.__orderedIndex[i+1]
            end
        end
    end

    if key then
        return key, t[key]
    end

    -- no more value to return, cleanup
    t.__orderedIndex = nil
    return
end

function orderedPairs(t)
    -- Equivalent of the pairs() function on tables. Allows to iterate
    -- in order
    return orderedNext, t, nil
end

function z.tostringordered(tabla, identacion)
    identacion = identacion or '\n'
    local identacion2 = identacion .. '  '
    
    if not tabla then
    	return
    end
    
    local valores = {}
    local k2, v2
 
    for k,v in orderedPairs(tabla) do
    	if type(k) == 'string' then
    		k2 = '"' .. k .. '"'
    	else
    		k2 = k
    	end
        if type(v) == 'table' then
            v2 = z.tostringordered(v, identacion2)
        elseif type(v)=='string' then
        	v2 = '"' .. v .. '"'
        else
        	v2 = tostring(v)
        end
        
        table.insert(valores, '[' .. k2 .. '] = ' .. v2)
    end
 
    return '{' .. identacion2 .. (table.concat(valores, ', ' .. identacion2) or '') .. identacion .. '}'
end

function z.tostring(tabla, identacion)
    identacion = identacion or '\n'
    local identacion2 = identacion .. '  '
    
    if not tabla then
    	return
    end
    
    local valores = {}
    local k2, v2
 
    for k,v in pairs(tabla) do
    	if type(k) == 'string' then
    		k2 = '"' .. k .. '"'
    	else
    		k2 = k
    	end
        if type(v) == 'table' then
            v2 = z.tostring(v, identacion2)
        elseif type(v)=='string' then
        	v2 = '"' .. v .. '"'
        else
        	v2 = tostring(v)
        end
        
        table.insert(valores, '[' .. k2 .. '] = ' .. v2)
    end
 
    return '{' .. identacion2 .. (table.concat(valores, ', ' .. identacion2) or '') .. identacion .. '}'
end

function z.elemento(tabla, indice1, indice2, indice3, indice4, indice5, indice6, indice7, indice8, indice9, indice10, indice11, indice12)
	local resultado
	
	if not tabla or not indice1 then
		return
	end
	resultado = tabla[indice1]
	
	if not indice2 or not resultado then
		return resultado
	end
	resultado = resultado[indice2]

	if not indice3 or not resultado then
		return resultado
	end
	resultado = resultado[indice3]
	
	if not indice4 or not resultado then
		return resultado
	end
	resultado = resultado[indice4]
	
	if not indice5 or not resultado then
		return resultado
	end
	resultado = resultado[indice5]
	
	if not indice6 or not resultado then
		return resultado
	end
	resultado = resultado[indice6]
	
	if not indice7 or not resultado then
		return resultado
	end
	resultado = resultado[indice7]
	
	if not indice8 or not resultado then
		return resultado
	end
	resultado = resultado[indice8]

	if not indice9 or not resultado then
		return resultado
	end
	resultado = resultado[indice9]
	
	if not indice10 or not resultado then
		return resultado
	end
	resultado = resultado[indice10]
	
	if not indice11 or not resultado then
		return resultado
	end
	resultado = resultado[indice11]
	
	if not indice12 or not resultado then
		return resultado
	end
	resultado = resultado[indice12]

	return resultado
end

function z.en(tabla, elemento)
    if not elemento then
        return
    end
    for k,v in pairs( tabla ) do
        if v == elemento then
            return k
        end
    end
end

function z.copiarElementosConValor(original)
	local copia= {}
	
    for k,v in pairs(original) do
        if v~='' then
            copia[k] = original[k]
        end
    end	
    
    return copia
end

function z.insertar(tabla, elemento)
	if not elemento then
		return false
	end

	if not z.en(tabla, elemento) then
		table.insert(tabla, elemento)	
	end
	
	return true
end

function z.insertarElementosConValor(origen, destino)
    for k,v in pairs(origen) do
        if v~='' then
            table.insert(destino, v)
        end
    end	
    
    return copia
end

function z.sonIguales(tabla1, tabla2)
	if not tabla1 or not tabla2 then
		return false
	end
	
	if tabla1 == tabla2 then
		return true
	end
	
	for k,v in pairs(tabla1) do
		if tabla2[k] ~= v then
			return false
		end
	end

	for k,v in pairs(tabla2) do
		if not tabla1[k] then
			return false
		end
	end

	return true
end

function z.ordenarFuncion(tabla, funcion)
	local funcionInestable = funcion
	
	-- Añadir a la tabla un campo con el orden
	for i,n in ipairs(tabla) do tabla[i].__orden = i end
	
	table.sort(tabla, 
		function(a,b) 
		   if     funcionInestable(a, b) then return true -- a < b
		   elseif funcionInestable(b, a) then return false -- b < a
		   elseif a.__orden < b.__orden  then return true -- a = b y a aparece antes que b
		   else                               return false -- a = b y b aparece antes que a
		   end
		end
	)
    
    -- Eliminar de la tabla el campo con el orden
	for i,n in ipairs(tabla) do tabla[i].__orden = nil end
end

function z.ordenar(tabla, criterio)
	if type(criterio) == 'table' then
		z.ordenarFuncion(tabla,
			function(a,b)
				local valorA, valorB
				for i,campo in ipairs(criterio) do
					valorA = a[campo]
					valorB = b[campo]
					if not valorA and not valorA then
						-- No hacer nada
					elseif not valorA and valorB then
						return true
					elseif valorA and not valorB then
						return false
					elseif valorA < valorB then
						return true
					elseif valorA > valorB then
						return false 
					end
				end	
		   		return false -- Todos los valores son iguales
			end
		)	
	else
		z.ordenarFuncion(tabla, criterio)
	end
end

function z.agrupar(tabla, clave, campo)
	local tabla2 = {}
	
	local v2
	
	for k,v in ipairs(tabla) do
		if not v[campo] then
			table.insert(tabla2, v)
			v2 = nil
		elseif v2 and v2[clave] == v[clave] then
			-- Agrupar
			table.insert(v2[campo], v[campo])
		else
			v2 = v
			v2[campo] = {v[campo]}	
			table.insert(tabla2, v2)
		end
	end
	
	return tabla2
end
 
return z