Lua table.getn() 返回 0? [英] Lua table.getn() returns 0?

查看:20
本文介绍了Lua table.getn() 返回 0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将 Lua 嵌入到我的 C 应用程序中,并试图通过以下方式找出为什么在我的 C 代码中创建了一个表:

I've embedded Lua into my C application, and am trying to figure out why a table created in my C code via:

lua_createtable(L, 0, numObjects);

并返回到 Lua,当我调用以下代码时将产生零结果:

and returned to Lua, will produce a result of zero when I call the following:

print("Num entries", table.getn(data))

(这里的data"是上面lua_createtable创建的表)

(Where "data" is the table created by lua_createtable above)

表格中清楚地有数据,因为我可以通过以下方式遍历每个条目(字符串:userdata)对:

There's clearly data in the table, as I can walk over each entry (string : userdata) pair via:

for key, val in pairs(data) do
  ...
end

但是为什么 table.getn(data) 返回零?当我用 lua_createtable 创建它时,我需要在表的元数据中插入一些东西吗?我一直在看 lua_createtable 使用的例子,但我没有在任何地方看到这样做过......

But why does table.getn(data) return zero? Do I need to insert something into the meta of the table when I create it with lua_createtable? I've been looking at examples of lua_createtable use, and I haven't seen this done anywhere....

推荐答案

table.getn(你不应该在 Lua 5.1+ 中使用它.使用长度运算符 #) 返回表的 array 部分中的元素数.

table.getn (which you shouldn't be using in Lua 5.1+. Use the length operator #) returns the number of elements in the array part of the table.

数组部分是从数字 1 开始并增加到 nil(不存在)的第一个值的每个键.如果你所有的键都是字符串,那么你表的数组部分的大小就是 0.

The array part is every key that starts with the number 1 and increases up until the first value that is nil (not present). If all of your keys are strings, then the size of the array part of your table is 0.

这篇关于Lua table.getn() 返回 0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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