Diferencia entre revisiones de «Módulo:Tablas»

De Hispanopedia
imported>Juan Mayordomo
Sin resumen de edición
imported>Juan Mayordomo
añadir un nuevo parámetro a la función elemento
Línea 27: Línea 27:
end
end


function z.elemento(tabla, indice1, indice2, indice3, indice4, indice5, indice6)
function z.elemento(tabla, indice1, indice2, indice3, indice4, indice5, indice6, indice7)
local resultado
local resultado
Línea 64: Línea 64:
end
end
resultado = resultado[indice6]
resultado = resultado[indice6]
if not indice7 or not resultado then
return resultado
end
resultado = resultado[indice7]
return resultado
return resultado

Revisión del 18:53 18 jun 2016

Uso

Todas las funciones del módulo se llaman desde Lua.

Funciones

Las funciones son:

tostring(tabla)
Convierte la tabla en una cadena de caracteres.
elemento(tabla, indice1, indice2, ..., indicen)
Devuelve el elemento tabla[indice1][indice2]...[indicen]
en(tabla, valor)
Devuelve la clave del elemento de tabla con ese valor si el elemento pertenece a la tabla.
insertar(tabla, elemento)
Inserta el elemento en la tabla si no está ya incluido.
Parámetros:
  • tabla: Tabla lua. Obligatorio.
  • elemento: Elemento a insertar. Optativo
Valor devuelto: La función devuelve true si el elemento a insertar está informado (no es nil) y false si no se le pasa ningún elemento a insertar.
copiarElementosConValor(tabla)
Devuelve una copia de la tabla eliminando los elementos sin valor
sonIguales(tabla1, tabla2)
Devuelve si las dos tablas son iguales.
ordenar(tabla, función)
Ordena la tabla utilizando la función. Similar a la función sort de table pero es estable.
agrupar(tabla, clave, campo)

ordenar(tabla, función)

Función para ordenar una tabla de forma estable. La función permite además ordenar valores nulos que se sitúan al principio de la tabla ordenada. Por ejemplo, si tenemos:

local moduloTablas = require('Módulo:Tablas') 
  local ordenarTabla = moduloTablas.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, campo)

Función para agrupar los elementos de la tabla con la misma clave. La tabla debe ordenarse previamente. En el ejemplo anterior el código:

  local agruparTabla = moduloTablas.agrupar

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

hará 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 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, 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 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.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)
end

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