[Question]Little Array Problem
[Question]Little Array Problem
How can I get the number of elements in an array?
Re: [Question]Little Array Problem
numberOfElements = #array
Note that the above only works if it's an array with consecutive integer indexes starting from 1; it won't count non-integer indexes or indexes less than or equal to 0, and it will stop at the first hole it encounters. E.g:
Note that the above only works if it's an array with consecutive integer indexes starting from 1; it won't count non-integer indexes or indexes less than or equal to 0, and it will stop at the first hole it encounters. E.g:
Code: Select all
> array = {1, 2, 3}
> array[5] = 4
> print(#array)
3
> array[4] = 5
> print(#array)
5
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Re: [Question]Little Array Problem
Thanks solved.Nixola wrote:numberOfElements = #array
Note that the above only works if it's an array with consecutive integer indexes starting from 1; it won't count non-integer indexes or indexes less than or equal to 0, and it will stop at the first hole it encounters. E.g:
Code: Select all
> array = {1, 2, 3} > array[5] = 4 > print(#array) 3 > array[4] = 5 > print(#array) 5
Re: [Question]Little Array Problem
Just a small correction: you can't rely on # to stop at the first hole. The only guarantee is that t[#t] ~= nil and t[#t+1] == nil. On the other hand, ipairs will definitely stop at the first hole.
Who is online
Users browsing this forum: slime and 3 guests