Lua and Lists

Started by
3 comments, last by quasty 19 years, 10 months ago
Hi, does LUA support some sort of lists with (fast) sort and search algorithms? thanx
Advertisement
Hi,

I''ve found out that the lua-table is the list of choice

Does anyone know a way to convert in C a std::list to a table?

thanx
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.

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.
wow - thanks a lot!

That works great

This topic is closed to new replies.

Advertisement