lua-users home
lua-l archive

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

I currently have something written in C++ which essentially amounts to
a double-keyed hash_map. I have exposed these functions to my Lua
scripts and I can set/get values in Lua code like so:

SetValue("key1", "key2", "value")
val = GetValue("key1", "key2)

I was thinking that it might be more natural to express this in Lua as
the following instead:

mymap["key1"]["key2] = "value"
val = mymap["key1"]["key2"]

I was thinking I might be able to exploit the __index and __newindex
metamethods to allow me to map/convert the latter form into the former
form. But I don't know how to write the metamethods for
multidimensional access (as all the examples presume single
dimensional access). Is it possible to do what I want in Lua? If so,
can somebody show me some example code of how to do this?