[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: help to (slightly) modify Lua interpreter
- From: Mark Feldman <mfeldman@...>
- Date: Fri, 06 Nov 2009 10:09:52 +1100
Francesco Abbate wrote:
I'm planning to translate the code like that:
m[i, j] => __index(m, i, j)
m[i, j] = v => __newindex(m, i, j, v)
So all the indexes will be feeded to the __index or __newindex
metamethods in the order.
Can you wrap the indices into their own seperate table, i.e. m[{1,2}]?
The code below uses only a single __index/__newindex handler but it
works for an arbitrary number of dimensions and could be modified to
pass control down through the heirarchy.
Mark Feldman
mt =
{
__index = function(table, indices)
if tonumber(indices) then
return rawget(table, indices)
else
for i=1,#indices-1 do
table = table[indices[i]]
end
return table[indices[#indices]]
end
end,
__newindex = function(table, indices, value)
if tonumber(indices) then
rawset(table, indices, value)
else
for i=1,#indices-1 do
table = table[indices[i]]
end
table[indices[#indices]] = value
end
end,
}
m = {}
setmetatable(m, mt)
m[{1}] = {}
m[{2}] = {}
print(m[{1}]) --> table: 00463F48
print(m[{2}]) --> table: 00463FF8
m[{1,2}] = "foo"
print(m[{1,2}]) --> foo
m[{3}] = {}
m[{3,4}] = {}
m[{3,4,5}] = "bar"
print(m[{3,4,5}]) --> bar
This message and its attachments may contain legally privileged or confidential information. This message is intended for the use of the individual or entity to which it is addressed. If you are not the addressee indicated in this message, or the employee or agent responsible for delivering the message to the intended recipient, you may not copy or deliver this message or its attachments to anyone. Rather, you should permanently delete this message and its attachments and kindly notify the sender by reply e-mail. Any content of this message and its attachments, which does not relate to the official business of the sending company must be taken not to have been sent or endorsed by the sending company or any of its related entities. No warranty is made that the e-mail or attachment(s) are free from computer virus or other defect.