使用Lua C API遍历表格 [英] Iterating over table of tables with the Lua C API

查看:176
本文介绍了使用Lua C API遍历表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遍历Lua中的表并输出:

I'm trying to iterate over a table of tables in Lua and output:

  • 每个表的键.
  • 每个表中每个条目的键/值对.

这是代码:

void print_table(lua_State *L)
{
    lua_pushnil(L);
    while(lua_next(L, -2) != 0) {
    const char *key = lua_tostring(L, -2);
            if(lua_isstring(L, -1))
                printf("%s = %s", key, lua_tostring(L, -1));
            else if(lua_isnumber(L, -1))
                printf("%s = %d", key, lua_tonumber(L, -1));
            else if(lua_istable(L, -1)) {
                printf("%s", key);
                PrintTable(L);
            }
            lua_pop(L, 1);
        }
    }
}

这是我要输出的表之一的示例:

And here is an example of one of a table I'm trying to output:

s = {
        p = {
            n = "D",
            g = "1",
        },
        d = {
            l = "N",
            p = "N",
            u = "O",
            po = 100,
        },
        e = {
            {
                n = "B",
                l = "P",
                p = "P",
                u = "P",
                po = "P",
                pa = {
                    v = "4",
                    a = "U",
                    f = {
                        { name = "U", type = "U" },
                        { name = "A", type = "I" },
                        { name = "A", type = "I" },
                        { name = "P", type = "U" },
                        { name = "P", type = "U" },
                        { name = "P", type = "I" },
                        { name = "T", type = "U" },
                        { name = "D", type = "U" },
                        { name = "D", type = "I" },
                        { name = "S", type = "I" },
                        { name = "C", type = "U" },
                        { name = "G", type = "U" },
                        { name = "C", type = "F" },
                        { name = "C", type = "U" },
                    },
                },
                c = {
                    v = "1",
                    a = "",
                    f = {
                        { name = "B", type = "U" },
                        { name = "E", type = "F" },
                    },
                },
            },
        },
    }

该函数在行中崩溃:

while(lua_next(L, -2) != 0)

由于索引无效.导致崩溃的脚本行是:

due to an invalid index. The line of script that causes the crash is:

{ name = "B", type = "U" },

我必须承认我对Lua中的堆栈不是很熟悉,我尝试搜索类似的答案,但找不到任何答案.有人知道我在做什么错吗?

I have to admit I'm not massively familiar with the stack in Lua, I've tried searching for similar answers and was unable to find any. Anyone know what I'm doing wrong?

谢谢!

添加了工作版本,以防有人感兴趣:

Added working version in case anyone is interested:

void print_table(lua_State *L)
    {
        if ((lua_type(L, -2) == LUA_TSTRING))
            printf("%s", lua_tostring(L, -2));

        lua_pushnil(L);
        while(lua_next(L, -2) != 0) {
            if(lua_isstring(L, -1))
                printf("%s = %s", lua_tostring(L, -2), lua_tostring(L, -1));
            else if(lua_isnumber(L, -1))
                printf("%s = %d", lua_tostring(L, -2), lua_tonumber(L, -1));
            else if(lua_istable(L, -1)) {
                print_table(L);
            }
            lua_pop(L, 1);
        }
    }

推荐答案

f = {
    { name = "B", type = "U" },
    { name = "E", type = "F" },
}

等效于:

f = {
    [1] = { name = "B", type = "U" },
    [2] = { name = "E", type = "F" },
}

当您在按键上调用lua_tostring时,Lua会将数字索引更改为字符串.

When you call lua_tostring on the key Lua changes the numeric index to a string.

const char *key = lua_tostring(L, -2);

lua_tostring使用lua_tolstring,并来自手册:

如果值是数字,则lua_tolstring还将堆栈中的实际值更改为字符串. (当在表遍历期间将lua_tolstring应用于键时,此更改会使lua_next感到困惑.)

If the value is a number, then lua_tolstring also changes the actual value in the stack to a string. (This change confuses lua_next when lua_tolstring is applied to keys during a table traversal.)

最好使用lua_type来检查键是否确实是字符串,因为lua_isstring仅会告诉您是否可以将堆栈值转换为字符串.您还可以推送密钥的副本,然后在副本上调用lua_tostring.

Better to use lua_type to check if the key really is a string since lua_isstring will only tell you if the stack value can be converted into a string. You could also push a copy of the key and call lua_tostring on the copy.

这篇关于使用Lua C API遍历表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆