如何在C API中创建嵌套的Lua表
2016-6-17 10:55:26
收藏:0
阅读:330
评论:3
我想用 C API 创建一个如下的表格:
myTable = {
[0] = { ["a"] = 4, ["b"] = 2 },
[1] = { ["a"] = 13, ["b"] = 37 }
}
我的当前方法是在循环中创建内部表格:
lua_createtable(L, 0, 2);
int c = lua_gettop(L);
lua_pushstring(L, "a");
lua_pushnumber(L, 4);
lua_settable(L, c);
lua_pushstring(L, "b");
lua_pushnumber(L, 2);
lua_settable(L, c);
在此循环之前,我使用以下代码来创建包含 2 个数字插槽的外部表格:
lua_createtable(L, 2, 0);
int outertable = lua_gettop(L);
但是,我该如何将内部表格保存到外部表格中?
原文链接 https://stackoverflow.com/questions/1630169
点赞
stackoverflow用户107090
对于你提供的简单代码,我的lua2c可以正常工作并生成以下代码。
/* 这个C代码是由下面的Lua代码使用lua2c生成的。
myTable = {
[0] = { ["a"] = 4, ["b"] = 2 },
[1] = { ["a"] = 13, ["b"] = 37 }
}
*/
static int MAIN(lua_State *L)
{
lua_newtable(L);
lua_pushnumber(L,0);
lua_newtable(L);
lua_pushliteral(L,"a");
lua_pushnumber(L,4);
lua_pushliteral(L,"b");
lua_pushnumber(L,2);
lua_settable(L,-5);
lua_settable(L,-3);
lua_pushnumber(L,1);
lua_newtable(L);
lua_pushliteral(L,"a");
lua_pushnumber(L,13);
lua_pushliteral(L,"b");
lua_pushnumber(L,37);
lua_settable(L,-5);
lua_settable(L,-3);
lua_settable(L,-5);
lua_settable(L,-3);
lua_setglobal(L,"myTable");
return 0;
}
2009-10-28 00:10:55
stackoverflow用户2413135
这是我为解决类似问题而想出的一种通用方法,基于lhf的回答。这会创建一个Lua表单,格式如下:
{
{"foo"},
{"bar", "baz"}
}
其中包含任意长度的表格/子表格。
int list_of_lists_to_lua(lua_State* L, const std::vector<std::vector<std::string>>& convertme) {
lua_newtable(L);
int counter = 0;
for (const std::vector<std::string>& list : convertme) {
lua_pushnumber(L, ++counter);
lua_newtable(L);
int counter2 = 0;
for (const std::string& item : list) {
lua_pushnumber(L, ++counter2);
lua_pushstring(L, item);
lua_settable(L,-3);
}
lua_settable(L,-3);
}
return 1;
}
2015-02-24 14:03:06
评论区的留言会收到邮件通知哦~
推荐文章
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
- 如何编写 Lua 模式将字符串(嵌套数组)转换为真正的数组?
下面是一个完整且简化的程序,演示了如何嵌套表格。基本上,您缺少的是
lua_setfield
函数。#include <stdio.h> #include "lua.h" #include "lauxlib.h" #include "lualib.h" int main() { int res; lua_State *L = lua_open(); luaL_openlibs(L); lua_newtable(L); /* 底层表 */ lua_newtable(L); /* 上层表 */ lua_pushinteger(L, 4); lua_setfield(L, -2, "four"); /* T[four] = 4 */ lua_setfield(L, -2, "T"); /* 将底层表的上层表字段命名为 T */ lua_setglobal(L, "t"); /* 设置底层表为全局变量 t */ res = luaL_dostring(L, "print(t.T.four == 4)"); if(res) { printf("错误: %s\n", lua_tostring(L, -1)); } return 0; }
该程序将简单地打印“true”。
如果需要数字索引,则继续使用
lua_settable
:#include <stdio.h> #include "lua.h" #include "lauxlib.h" #include "lualib.h" int main() { int res; lua_State *L = lua_open(); luaL_openlibs(L); lua_newtable(L); /* 底层表 */ lua_newtable(L); /* 上层表 */ lua_pushinteger(L, 0); lua_pushinteger(L, 4); lua_settable(L, -3); /* 将 4 存入上层表索引为 0 的位置;弹出 0 和 4 */ lua_pushinteger(L, 0); lua_insert(L, -2); /* 将上层表(-2)与 0 交换位置 */ lua_settable(L, -3); /* 底层表的索引为 0 的位置存放上层表 */ lua_setglobal(L, "t"); /* 设置底层表为全局变量 t */ res = luaL_dostring(L, "print(t[0][0] == 4)"); if(res) { printf("错误: %s\n", lua_tostring(L, -1)); } return 0; }
与我所用的绝对索引 0 不同,您可能需要使用
lua_objlen
生成索引。