What does # mean in Lua?

前端 未结 1 1207
感情败类
感情败类 2020-12-14 14:54

I have seen the hash character \'#\' being added to the front of variables a lot in Lua.

What does it do?

EXAMPLE

-- sort AIs in currentleve         


        
相关标签:
1条回答
  • 2020-12-14 15:03

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

    0 讨论(0)
提交回复
热议问题