lua-users home
lua-l archive

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


Eike Decker wrote:
>> Can you specify how you want to access your matrix?
> 
> like
> 
> mymatrix["bar"]["tex"] = "x"
> 
> or
> 
> mymatrix[1][2] = "foo"

The normal solution is to allocate intermediate tables yourself:

mymatrix["bar"] = {}
mymatrix["bar"]["tex"] = "x"
mymatrix[1] = {}
mymatrix[1][2] = "foo"

The magic solution is to automtically create a subtable when you access your root table. To do that use metatables:

mymatrix = setmetatable({}, {
    __index = function(t,k)
        local subt = {}
        t[k] = subt
        return subt
    end
})

mymatrix["bar"]["tex"] = "x"
mymatrix[1][2] = "foo"