I know how for loops work and I use them quite often but also seem to often come across a # in others' code and I want to know what it is for and how to use it. An example of this would be:
for i = 1, #npc do local v = npc[i]
I cant seem to find anything online regarding this, maybe my searches just aren't good but it would be nice if someone could explain it for me, thanks.
In Lua, #
is the length operator. for i = 1, #npc
essentially loops from 1 to the length of the npc
array.
As was already pointed out, it gets the length of a list. However, there's another thing worth pointing out: that for
loop is suboptimal and unidiomatic. It would be better written as for i, v in ipairs(npc) do
. In general, using #
in a for
loop is almost always the wrong thing to do.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.