lua - How can I change every index into a table using a metatable? -
i'm trying write metatable indexes table shifted 1 position (i.e. t[i]
should return t[i+1]
). need because table defined using index 1 first element, have interface program uses index 0 first element. since reading programming in lua, think can accomplish want proxy table, can't seem working. far, have this:
t = {"foo", "bar"} local _t = t t = {} local mt = { __index = function(t, i) return _t[i+1] end } setmetatable(t, mt)
however, not create expected result. in fact, doesn't return values @ (every lookup nil
). there better way this, or missing something?
t = {"foo", "bar"} local _t = t t = {} local mt = { __index = function(t, i) return _t[i+1] end } setmetatable(t, mt) print(t[0])
outputs "foo" me when run here: http://www.lua.org/cgi-bin/demo
Comments
Post a Comment