[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: [Newbie] Index a matrix with strings
- From: "Jerome Vuarand" <jerome.vuarand@...>
- Date: Thu, 8 Nov 2007 11:21:15 -0500
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"