Diferencia entre revisiones de «Módulo:Tablas»
Sin resumen de edición |
m 48 revisiones importadas |
||
| (No se muestran 10 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 | 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=' | k2 = '"' .. k .. '"' | ||
else | else | ||
k2= | k2 = k | ||
end | end | ||
if type(v)=='table' then | if type(v) == 'table' then | ||
v2 = z.tostring(v, identacion2) | |||
elseif type(v)=='string' then | elseif type(v)=='string' then | ||
v2 = '"' .. v .. '"' | |||
else | else | ||
v2 = tostring(v) | |||
end | end | ||
table.insert(valores, '[' .. k2 .. '] = ' .. v2) | |||
end | end | ||
return | return '{' .. identacion2 .. (table.concat(valores, ', ' .. identacion2) or '') .. identacion .. '}' | ||
end | end | ||
| Línea 148: | Línea 229: | ||
-- Añadir a la tabla un campo con el orden | -- Añadir a la tabla un campo con el orden | ||
for i,n in ipairs(tabla) do tabla[i]. | for i,n in ipairs(tabla) do tabla[i].__orden = i end | ||
table.sort(tabla, | table.sort(tabla, | ||
| Línea 154: | Línea 235: | ||
if funcionInestable(a, b) then return true -- a < b | if funcionInestable(a, b) then return true -- a < b | ||
elseif funcionInestable(b, a) then return false -- b < a | elseif funcionInestable(b, a) then return false -- b < a | ||
elseif 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 | else return false -- a = b y b aparece antes que a | ||
end | end | ||
end | |||
) | |||
-- Eliminar de la tabla el campo con el orden | |||
for i,n in ipairs(tabla) do tabla[i].__orden = nil end | |||
end | end | ||
| Línea 164: | Línea 249: | ||
z.ordenarFuncion(tabla, | 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 | ||
valorA = a[campo] | |||
elseif | 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 | ||