创建和Lua表C API一个简单的表 [英] Creating a simple table with Lua tables C API
问题描述
我运行一个MySQL查询它总是返回4行:
按行>名称
,按行>日期
,按行&GT ; IP
,按行>定制
我要实现的是创建上述结果的简单表筑底所以它看起来像什么:
{
名=按行的GT和结果;名称,
日期按行=的制造&gt结果;日期,
IP=行向&GT的结果; IP,
定制按行=的制造&gt结果;定制
}
我曾尝试多种可能性,但张贴的例子是真是五花八门,我得到了一个问题,使得它的工作。
我最后不成功的尝试:
lua_createtable(L,0,4);
顶= lua_gettop(L);
lua_pushstring(L,姓名);
lua_pushstring(L,按行>名);
lua_pushstring(L,日期);
调用lua_pushnumber(L,按行>日期);
lua_pushstring(L,IP);
lua_pushstring(L,按行> IP);
lua_pushstring(L,定制);
lua_pushstring(L,按行>自定);
lua_settable(L,顶部);
正如我在评论中提及, lua_settable()
照顾只有一个键,值
对。必须重申,如果你需要更多。
我preFER保存Lua的堆栈空间是这样的:
lua_createtable(L,0,4);lua_pushstring(L,姓名);
lua_pushstring(L,按行>名);
lua_settable(L,-3); / *从栈顶元素3 * /lua_pushstring(L,日期);
lua_pushstring(L,按行>日期);
lua_settable(L,-3);lua_pushstring(L,IP);
lua_pushstring(L,按行> IP);
lua_settable(L,-3);lua_pushstring(L,定制);
lua_pushstring(L,按行>自定);
lua_settable(L,-3);/ *我们仍然有留在Lua的堆栈的顶部表。 * /
另外,你可以写一些C结构迭代器之类的东西。
请注意:如果这是某种形式的Lua包装的 - 你应该确保标准化这样做方式。在下面的示例应用 @lhf 评论约缩短了一点:
INT
l_row_push(lua_State * L)
{
lua_createtable(L,0,4); / *创建和Lua的堆栈顶部推新表* / lua_pushstring(L,按行>名); / *上的Lua堆栈顶部推表值* /
lua_setfield(L,-2,名); / *表[名称] =按行>的名字。持久性有机污染物的键值* / lua_pushstring(L,按行>日期);
lua_setfield(L,-2,日期); lua_pushstring(L,按行> IP);
lua_setfield(L,-2,IP); lua_pushstring(L,按行>自定);
lua_setfield(L,-2,定制); / *返回一个表这已经是对的Lua堆栈的顶部。 * /
返回1;
}
编辑:修正 的使用lua_setfield()
以 @lhf 注的。谢谢!
I'm running a MySQL query which always returns 4 rows:
row->name
, row->date
, row->ip
, row->custom
What I want to achieve is create a simple table basing on the above results so it would look like:
{
"name" = result of row->name,
"date" = result of row->date,
"ip" = result of row->ip,
"custom" = result of row->custom
}
I have tried multiple possibilities, but the examples posted are really varied and I got a problems making it working.
My last unsuccessful try:
lua_createtable(L, 0, 4);
top = lua_gettop(L);
lua_pushstring(L, "name");
lua_pushstring(L, row->name);
lua_pushstring(L, "date");
lua_pushnumber(L, row->date);
lua_pushstring(L, "ip");
lua_pushstring(L, row->ip);
lua_pushstring(L, "custom");
lua_pushstring(L, row->custom);
lua_settable(L, top);
As I mentioned in comment, lua_settable()
takes care only of one key, value
pair. Must repeat that if You need more.
I'd prefer saving the Lua stack space like this:
lua_createtable(L, 0, 4);
lua_pushstring(L, "name");
lua_pushstring(L, row->name);
lua_settable(L, -3); /* 3rd element from the stack top */
lua_pushstring(L, "date");
lua_pushstring(L, row->date);
lua_settable(L, -3);
lua_pushstring(L, "ip");
lua_pushstring(L, row->ip);
lua_settable(L, -3);
lua_pushstring(L, "custom");
lua_pushstring(L, row->custom);
lua_settable(L, -3);
/* We still have table left on top of the Lua stack. */
Also, You could write some kind of C struct iterator or something.
NOTE: if this is for some kind of Lua wrapper - You should ensure standardized way of doing that. In the following example applied @lhf comment about shortening it a bit:
int
l_row_push(lua_State *l)
{
lua_createtable(L, 0, 4); /* creates and pushes new table on top of Lua stack */
lua_pushstring(L, row->name); /* Pushes table value on top of Lua stack */
lua_setfield(L, -2, "name"); /* table["name"] = row->name. Pops key value */
lua_pushstring(L, row->date);
lua_setfield(L, -2, "date");
lua_pushstring(L, row->ip);
lua_setfield(L, -2, "ip");
lua_pushstring(L, row->custom);
lua_setfield(L, -2, "custom");
/* Returning one table which is already on top of Lua stack. */
return 1;
}
EDIT: Fixes usage of lua_setfield()
by @lhf note. Thanks!
这篇关于创建和Lua表C API一个简单的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!