Módulo:Táboas

Esta páxina está semiprotexida
Na Galipedia, a Wikipedia en galego.
Indicacións de uso do módulo

Uso[editar a fonte]

{{#invoke:táboas|función|primeiro elemento|segundo elemento|...|último elemento|parámetros opcionais}}

Funcións[editar a fonte]

As funcións son:

tostring(táboa)
Convirte a táboa nunha cadea de caracteres.
elemento(táboa, indice1, indice2, ..., indicen)
Devolve o elemento táboa[indice1][indice2]...[indicen]
en(táboa, valor)
Devolve a clave do elemento de táboa con ese valor se o elemento pertence á táboa.
insertar(táboa, elemento)
Insire o elemento na táboa se non está xa incluído.
copiarElementosConValor(táboa)
Devolve unha copia da táboa eliminando os elementos sen valor
ordenar(táboa, función)
Ordena a táboa utilizando a función. Similar á función sort de table pero é estable.
sonIguales(táboa1, táboa2)
Devolve se as dúas táboas son iguais.

Esta documentación está transcluída desde Módulo:Táboas/uso. Os editores poden probar cambios no mesmo en Módulo:Táboas/probas.
Por favor, engade as categorías na subpáxina de documentación e os interwikis no Wikidata. Ver as subpáxinas deste módulo.

--[[*********************************************************************************
    * Nome: Módulo:Táboas
    *
    * Descrición: 
    *
    * Data última revisión: 
    *if 
    * Estado: En uso. 
    * Nota: Adaptado de es.wiki
    *********************************************************************************`-- ]]

local z = {}

function z.tostring(tabla, identacion)
    identacion = identacion or '\n'
    local resultado = ''
    
    if not tabla then
    	return
    end
 
    for k,v in pairs(tabla) do
    	if type(k) == 'string' then
    		k2='"' .. k .. '"'
    	else
    		k2=k
    	end
        if type(v)=='table' then
            resultado = resultado .. identacion .. k2 .. ': {' .. z.tostring(v,identacion .. '  ') .. identacion .. '}'
        elseif type(v)=='string' then
        	resultado = resultado .. identacion .. k2 .. ': "' .. v .. '"'
        else
        	resultado = resultado .. identacion .. k2 .. ': ' .. tostring(v)
        end
    end
 
    return resultado
end

function z.elemento(tabla, indice1, indice2, indice3, indice4, indice5)
	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]	
	
	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 z.en(tabla, elemento) then
		table.insert(tabla, elemento)
	end
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.ordenar(tabla, funcion)
	local funcionInestable = funcion
	
	-- Engadir á táboa un campo coa orde
	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 e a aparece antes que b
		   else                               return false -- a = b e b aparece antes que a
		   end
	    end)
end
 
return z