Lua,表的大小(#table_name 或 table.getn(table_name),有时返回 0

标签 lua

#table_name返回非0值时? 再说一遍,当 table.getn(table_name) 确实返回非 0 值时?

最佳答案

仔细阅读手册:

2.5.5 - The Length Operator

The length operator is denoted by the unary operator #. The length of a string is its number of bytes (that is, the usual meaning of string length when each character is one byte).

The length of a table t is defined to be any integer index n such that t[n] is not nil and t[n+1] is nil; moreover, if t[1] is nil, n can be zero. For a regular array, with non-nil values from 1 to a given n, its length is exactly that n, the index of its last value. If the array has "holes" (that is, nil values between other non-nil values), then #t can be any of the indices that directly precedes a nil value (that is, it may consider any such nil value as the end of the array).

关于Lua,表的大小(#table_name 或 table.getn(table_name),有时返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10718273/

相关文章:

lua uart.alt(1) 如何使其工作

unit-testing - 如何在单个文件 Lua 脚本中测试函数?

lua - 我可以从通过 string.dump() 检索的 lua 字节码中得到什么?

c++ - Linux、C++、Lua 5.3、CMake、未定义引用

lua - 硬编码的数学运算是在lua编译时还是运行时执行的

c - 将 C 整数绑定(bind)到 Lua 全局变量

objective-c - 尝试在 iPhone-Wax 中创建 NSDecimal

variables - 在Lua中不等于或等于

带有字符串键的 Lua 表不能使用数字索引?

multithreading - Lua 操作,适用于多头环境