Archived

This topic is now archived and is closed to further replies.

Lua and Lists

This topic is 4941 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What you need to do is iterate through the list and push the key and value onto the table. Indexes start at 1 in lua, so something like this should work (pseudocode wise):

lua_State *L

lua_newtable(L);
int i = 1;
for(somelist it=begin; it!=end; ++it)
lua_pushnumber(i); // Push index of element
lua_pushstring(it); // Or whatever datatype you want to push
lua_settable(L, -3); // Set the key/value pair to the table located three positions down from the top of the stack.

Share this post


Link to post
Share on other sites
If you use Luabind it''s easy.

Say you have a class A with a list member.


class A {
list<string> values;
};


Then just compile this code:


lua_State* L = lua_open();
luabind::open(L);
module(L)
[
class_<A>("A")
.def(constructor<>())
.def_readwrite("values",&A:values,return_stl_iterator)
];


Then your Lua code could look like this:


local obj = A()
local t = {}
for v in obj.values do
table.insert(t,v)
end


Now t contains all the values of names.

Share this post


Link to post
Share on other sites