That appears to be the case. But then "length" is a bad name as well because array could be longer than this value. So the only way to reliably iterate through array with holes is to use "pairs". Then again, it ignores holes and includes literal indices. Alternatively, you keep track of its real length elsewhere.It's actually the index of any element preceding a nil
BulletManager (performance argument)
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: BulletManager (performance argument)
Re: BulletManager (performance argument)
In further support of this, in LuaJIT:airstruck wrote:No, it isn't. It's actually the index of any element preceding a nil (which may or may not be the highest numbered integer indexed element). The manual says so in the first sentence of the paragraph explaining the behavior of the length operator on tables.
Code: Select all
print(#{1, 2, nil, 4, 5}) -- prints 5
print(#{1, 2, 3, nil, 5}) -- prints 3
Re: BulletManager (performance argument)
Bad or not, that's still its name. If you have a better suggestion for a name, pitch it to the Lua authors. It would be confusing if users started calling it by something other than its name.raidho36 wrote:But then "length" is a bad name as well because array could be longer than this value.
Also, an array can't be longer than this value if "length" in this context simply means the index of any non-nil element preceding a nil, and nothing more. The word can mean whatever the authors want it to mean; they probably just found it to be a good fit for that concept (I've never seen any better suggestions, anyway).
And the order of iteration is not defined, and it won't JIT.So the only way to reliably iterate through array with holes is to use "pairs". Then again, it ignores holes and includes literal indices.
Yeah, that's the usual approach to storing sparse arrays. Generally the "real" length is stored in the same table in a key named "n" (which has some historical precedent because of its special meaning to the deprecated/removed "table.getn" and "table.setn" functions).Alternatively, you keep track of its real length elsewhere.
Right, and it's implementation-dependent; as I recall PUC Lua gives different results in at least one of those cases. Anyway, bottom line is # won't give predictable results on sparse arrays.pgimeno wrote:In further support of this, in LuaJIT:Code: Select all
print(#{1, 2, nil, 4, 5}) -- prints 5 print(#{1, 2, 3, nil, 5}) -- prints 3
Last edited by airstruck on Sun Aug 28, 2016 8:13 pm, edited 1 time in total.
Re: BulletManager (performance argument)
Furthermorepgimeno wrote: In further support of this, in LuaJIT:Code: Select all
print(#{1, 2, nil, 4, 5}) -- prints 5 print(#{1, 2, 3, nil, 5}) -- prints 3
Code: Select all
foo = {1, 2, 3}
print(#foo) -- prints 3
foo[10] = 10
print(#foo) --prints 3 also
Code: Select all
print (#{ [1000] = 1, [1010] = 2, [1020] = 3 }) --prints 0
Who is online
Users browsing this forum: Semrush [Bot] and 10 guests