Módulu:Tables

De Wikipedia

Usu[editar la fonte]

Toles funciones del módulu llámense desde Lua.

Funciones[editar la fonte]

Les funciones son:

tostring(tabla)
Convierte la tabla nuna cadena de caráuteres.
elementu(tabla, indice1, indice2, ..., indicen)
Devuelve l'elementu tabla[indice1][indice2]...[indicen]
en(tabla, valor)
Devuelve la clave del elementu de tabla con esi valor si l'elementu pertenez a la tabla.
insertar(tabla, elementu)
Inserta l'elementu na tabla si nun ta incluíu yá.
Parámetros:
  • tabla: Tabla lua. Obligatoriu.
  • elementu: Elementu a inxertar. Optativu
Valor devueltu: La función devuelve true si l'elementu a inxertar ta informáu (nun ye nil) y false si nun se pasa nengún elementu a inxertar.
copiarElementosConValor(tabla)
Devuelve una copia de la tabla desaniciando los elementos sin valor
sonIguales(tabla1, tabla2)
Devuelve si les dos tables son iguales.
ordenar(tabla, función)
Ordena la tabla utilizando la función. Asemeyada a la función sort de table pero ye estable.
agrupar(tabla, clave, campu)

ordenar(tabla, función)[editar la fonte]

Función pa ordenar una tabla de forma estable. La función permite amás ordenar valores nulos que tean al principiu de la tabla ordenada. Por casu, si tenemos:

local moduluTables = require('Módulu:Tables') 
  local ordenarTabla = moduluTables.ordenar

local t = {
  [1] = {
    ["id"] = "Q255032", 
    ["anyo"] = "1965"
  }, 
  [2] = {
    ["id"] = "Q936683", 
    ["anyo"] = "1967"
  }, 
  [3] = {
    ["id"] = "Q1056265", 
    ["anyo"] = "1968"
  }, 
  [4] = {
    ["id"] = "Q1086189", 
    ["anyo"] = "1970"
  }, 
  [5] = {
    ["id"] = "Q549884", 
    ["anyo"] = "1970"
  }, 
  [6] = {
    ["id"] = "Q549884", 
    ["anyo"] = "1971"
  }, 
  [7] = {
    ["id"] = "Q610903", 
    ["anyo"] = "1975"
  }, 
  [8] = {
    ["id"] = "Q1056251", 
    ["anyo"] = "1976"
  }, 
  [9] = {
    ["id"] = "Q3405406", 
    ["anyo"] = "1976"
  }, 
  [10] = {
    ["id"] = "Q3625727", 
    ["anyo"] = "1976"
  }, 
  [11] = {
    ["id"] = "Q898527", 
    ["anyo"] = "1978"
  }, 
  [12] = {
    ["id"] = "Q901462", 
    ["anyo"] = "1981"
  }, 
  [13] = {
    ["id"] = "Q3910469", 
    ["anyo"] = "1987"
  }, 
  [14] = {
    ["id"] = "Q255032", 
    ["anyo"] = "1958"
  }
}

local t2 = ordenarTabla(t, {'id', 'anyo'})

t2 será igual a:

{
  [1] = {
    ["anyo"] = "1976", 
    ["id"] = "Q1056251"
  }, 
  [2] = {
    ["anyo"] = "1968", 
    ["id"] = "Q1056265"
  }, 
  [3] = {
    ["anyo"] = "1970", 
    ["id"] = "Q1086189"
  }, 
  [4] = {
    ["anyo"] = "1958", 
    ["id"] = "Q255032"
  }, 
  [5] = {
    ["anyo"] = "1965", 
    ["id"] = "Q255032"
  }, 
  [6] = {
    ["anyo"] = "1976", 
    ["id"] = "Q3405406"
  }, 
  [7] = {
    ["anyo"] = "1976", 
    ["id"] = "Q3625727"
  }, 
  [8] = {
    ["anyo"] = "1987", 
    ["id"] = "Q3910469"
  }, 
  [9] = {
    ["anyo"] = "1970", 
    ["id"] = "Q549884"
  }, 
  [10] = {
    ["anyo"] = "1971", 
    ["id"] = "Q549884"
  }, 
  [11] = {
    ["anyo"] = "1975", 
    ["id"] = "Q610903"
  }, 
  [12] = {
    ["anyo"] = "1978", 
    ["id"] = "Q898527"
  }, 
  [13] = {
    ["anyo"] = "1981", 
    ["id"] = "Q901462"
  }, 
  [14] = {
    ["anyo"] = "1967", 
    ["id"] = "Q936683"
  }
}

agrupar(tabla, clave, campu)[editar la fonte]

Función p'agrupar los elementos de la tabla cola mesma clave. La tabla tien d'ordenase previamente. Nel exemplu anterior el códigu:

  local agruparTabla = moduluTables.agrupar

local t3 = agrupar(t2, 'id', 'anyo')

va facer que la tabla t3 valga:

{
  [1] = {
    ["anyo"] = {
      [1] = "1976"
    }, 
    ["id"] = "Q1056251"
  }, 
  [2] = {
    ["anyo"] = {
      [1] = "1968"
    }, 
    ["id"] = "Q1056265"
  }, 
  [3] = {
    ["anyo"] = {
      [1] = "1970"
    }, 
    ["id"] = "Q1086189"
  }, 
  [4] = {
    ["anyo"] = {
      [1] = "1958", 
      [2] = "1965"
    }, 
    ["id"] = "Q255032"
  }, 
  [5] = {
    ["anyo"] = {
      [1] = "1976"
    }, 
    ["id"] = "Q3405406"
  }, 
  [6] = {
    ["anyo"] = {
      [1] = "1976"
    }, 
    ["id"] = "Q3625727"
  }, 
  [7] = {
    ["anyo"] = {
      [1] = "1987"
    }, 
    ["id"] = "Q3910469"
  }, 
  [8] = {
    ["anyo"] = {
      [1] = "1970", 
      [2] = "1971"
    }, 
    ["id"] = "Q549884"
  }, 
  [9] = {
    ["anyo"] = {
      [1] = "1975"
    }, 
    ["id"] = "Q610903"
  }, 
  [10] = {
    ["anyo"] = {
      [1] = "1978"
    }, 
    ["id"] = "Q898527"
  }, 
  [11] = {
    ["anyo"] = {
      [1] = "1981"
    }, 
    ["id"] = "Q901462"
  }, 
  [12] = {
    ["anyo"] = {
      [1] = "1967"
    }, 
    ["id"] = "Q936683"
  }
}

local z = {}

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)
	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]
	
	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
	
	-- Amestar a la table un campu col 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 y a apaez enantes que b
		   else                               return false -- a = b y b apaez enantes qu'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 -- Tolos 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