Diferencia entre revisiones de «Módulo:Tablas»

Permitir ordenar por el nombre de los campos
m 48 revisiones importadas
 
(No se muestran 19 ediciones intermedias de 6 usuarios)
Línea 1: Línea 1:
local z = {}
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)
function z.tostring(tabla, identacion)
     identacion = identacion or '\n'
     identacion = identacion or '\n'
     local resultado = ''
     local identacion2 = identacion .. ' '
      
      
     if not tabla then
     if not tabla then
     return
     return
     end
     end
   
    local valores = {}
    local k2, v2
   
   
     for k,v in pairs(tabla) do
     for k,v in pairs(tabla) do
     if type(k) == 'string' then
     if type(k) == 'string' then
     k2='"' .. k .. '"'
     k2 = '"' .. k .. '"'
     else
     else
     k2=k
     k2 = k
     end
     end
         if type(v)=='table' then
         if type(v) == 'table' then
             resultado = resultado .. identacion .. k2 .. ': {' .. z.tostring(v,identacion .. '  ') .. identacion .. '}'
             v2 = z.tostring(v, identacion2)
         elseif type(v)=='string' then
         elseif type(v)=='string' then
         resultado = resultado .. identacion .. k2 .. ': "' .. v .. '"'
         v2 = '"' .. v .. '"'
         else
         else
         resultado = resultado .. identacion .. k2 .. ': ' .. tostring(v)
         v2 = tostring(v)
         end
         end
       
        table.insert(valores, '[' .. k2 .. '] = ' .. v2)
     end
     end
   
   
     return resultado
     return '{' .. identacion2 .. (table.concat(valores, ', ' .. identacion2) or '') .. identacion .. '}'
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 145:
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
Línea 93: Línea 180:


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


Línea 132: Línea 225:
end
end


function z.ordenar(tabla, citerio)
function z.ordenarFuncion(tabla, funcion)
if type(criterio) == 'function' then
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
local funcionInestable = criterio
table.sort(tabla,  
-- Añadir a la tabla un campo con el orden
function(a,b)  
for i,n in ipairs(tabla) do tabla[i].__orden = i end
  if    funcionInestable(a, b) then return true -- a < b
  elseif funcionInestable(b, a) then return false -- b < a
table.sort(tabla,  
  elseif a.orden < b.orden      then return true -- a = b y a aparece antes que b
function(a,b)  
  else                              return false -- a = b y b aparece antes que a
  if    funcionInestable(a, b) then return true -- a < b
  end
  elseif funcionInestable(b, a) then return false -- b < a
    end
  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
else
  end
return z.ordenar(tabla,
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)
function(a,b)
local valorA, valorB
for i,campo in ipairs(criterio) do
for i,campo in ipairs(criterio) do
if a[campo] < b[campo] then return true
valorA = a[campo]
elseif a[campo] > b[campo] then return false  
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
end
end
  return false -- Todos los valores son iguales
  return false -- Todos los valores son iguales
end
end
)
)
else
z.ordenarFuncion(tabla, criterio)
end
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
end
   
   
return z
return z