I'm trying to create this datastructure from C:
local table = {
id = 12,
items = {
{
id = 1,
name = "foo",
},{
id = 2,
name = "bar",
}
}
}
However I don't manage to get the anonymous tables working. (It's an array I want but array and tables is the same in lua afaik).
lua_newtable(L);
lua_pushinteger(L, 1);
lua_setfield(L, -2, "id");
lua_newtable(L);
lua_newtable(L);
lua_pushinteger(L, 1);
lua_setfield(L, -2, "id");
lua_pushstring(L, "foo");
lua_setfield(L, -2, "name");
lua_setfield(L, -2, "1");
lua_newtable(L);
lua_pushinteger(L, 1);
lua_setfield(L, -2, "id");
lua_pushstring(L, "bar");
lua_setfield(L, -2, "name");
lua_setfield(L, -2, "2");
lua_setfield(L, -2, "items");
And this gives
{
id = 1,
items = {
["1"] = {
id = 1,
name = "foo"
},
["2"] = {
id = 1,
name = "bar"
}
}
}
I'm using lua 5.1 so I don't have access to lua_seti