3

什么#table_name时候返回非 0 值?再说一次,什么table.getn(table_name)时候返回非 0 值?

4

2 回答 2

7

Go through the manual:

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

于 2012-05-23T10:44:57.377 回答
3

如果表中的键不是整数(例如字符串),则长度运算符将返回 0。

于 2012-05-25T13:30:24.427 回答