lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


(...). I'm getting caught, however, when it comes to deleting elements
> from the table.  (...)

Just assign 'nil' to the desired key:

local t = { a = 1, b = 2 }
print(t.a, t.b)
> 1	2
t.a = nil
print(t.a, t.b)
> nil	2

	While I'm asking questions, I wonder if perhaps there is a better way
to save the table to a file. (...)

It depends on what you want to save and how to save. I use this function which solves most of my needs (specially for debugging):

function serialize(value)
    local t = type(value)

        if t=='string'  then return string.format('%q',value)
    elseif t=='number'  then return tostring(value)
    elseif t=='boolean' then return value and 'true' or 'false'
    elseif t=='table'   then
        local buf = '{'
        for key,val in pairs(value) do
            buf = buf..'['..serialize(key)..']='..serialize(val)..','
        end
        return buf .. '}'
    else return '!UNKNOW!' -- here you could handle userdata...
    end
end


--rb