Metatables and multidimensional arrays

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Metatables and multidimensional arrays

Eric Wing
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?

Thanks,
Eric
Reply | Threaded
Open this post in threaded view
|

Re: Metatables and multidimensional arrays

Luiz Henrique de Figueiredo
> 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?

Try http://lua-users.org/wiki/AutomagicTables