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

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

问题描述

我的Lua嵌入到我的C应用程序,并正尝试找出为什么通过我的C code创建的表:

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))

(这里的数据是lua_createtable创建的表)

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

有显然数据的的表,我可以在每个条目走(字符串:用户数据),通过对:

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(数据)返回零?我需要的东西插入到表的元当我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+,使用长度操作符)返回元素的数量的阵列的表的一部分。

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开始,并增加了,直到那就是(未present)的第一个值的关键。如果所有的按键都是字符串,那么你的表的阵列部分的大小为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天全站免登陆