Diferencia entre revisiones de «Módulo:Tablas»

Hacer que la función tostring ordene las claves
m 48 revisiones importadas
 
(No se muestran 7 ediciones intermedias de 6 usuarios)
Línea 44: Línea 44:
     -- in order
     -- in order
     return orderedNext, t, nil
     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
end


Línea 57: Línea 88:
     local k2, v2
     local k2, v2
   
   
     for k,v in orderedPairs(tabla) do
     for k,v in pairs(tabla) do
     if type(k) == 'string' then
     if type(k) == 'string' then
     k2 = '"' .. k .. '"'
     k2 = '"' .. k .. '"'