#在Lua中是什么意思? [英] What does # mean in Lua?

查看:152
本文介绍了#在Lua中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Lua中,我已经看到很多哈希字符'#'被添加到变量的前面.

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

它是做什么的?

示例

-- sort AIs in currentlevel
table.sort(level.ais, function(a,b) return a.y < b.y end)
local curAIIndex = 1
local maxAIIndex = #level.ais
for i = 1,#currentLevel+maxAIIndex do
    if level.ais[curAIIndex].y+sprites.monster:getHeight() < currentLevel[i].lowerY then
        table.insert(currentLevel, i, level.ais[curAIIndex])
        curAIIndex = curAIIndex + 1
        if curAIIndex > maxAIIndex then
            break
        end
    end
end

很抱歉,如果已经问过这个问题,我已经在互联网上搜索了很多,但似乎还没有找到答案.预先感谢!

Apologies if this has already been asked, I've searched around on the internet a lot but I haven't seem to have found an answer. Thanks in advance!

推荐答案

长度运算符由一元运算符#表示.字符串的长度是其字节数(即,每个字符为一个字节时字符串长度的通常含义).

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

表t的长度定义为任何整数索引n,以使t [n]不为nil,t [n + 1]为nil;此外,如果t [1]为nil,则n可以为零.对于具有从1到给定n的非零值的常规数组,其长度恰好是n(其最后一个值的索引).如果数组具有空洞"(即其他非nil值之间的nil值),则#t可以是直接在nil值之前的任何索引(即,它可以将任何此类nil值视为结尾)的数组).

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中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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