- Joined
- Mar 26, 2017
- Messages
- 376
Just wanted to mention something about the # operator that was not working as expected. I use this for my systems to efficiently manage data in the map. I DON'T want to have to iterate over (long) lists each time.
Now what I expected was that # returns the last not-nil value in a straight array.
But unfortunately, it doesn't work with setting values to nil. Like it's not updating its internal statistic.
Example:
Somehow, nilling a certain value ends up updating the statistic. So if you do something like this:
I'm a little bit stuck here, because if the table is not creating like l = {11,22,33,44,55}, but instead like this:
l = {}
for x=1,5 do l[x]=x*11 end
Now suddenly not nilling element 5, but element 4 updates the statistics.
Meanwhile I'm experimenting a bit, but could someone with insight in lua chime in, and help me find the most efficient way to find a non-nil value in a straight array
Now what I expected was that # returns the last not-nil value in a straight array.
But unfortunately, it doesn't work with setting values to nil. Like it's not updating its internal statistic.
Example:
Lua:
l = {11,22,33,44,55}
l[3] = nil
print(#l) >returns 5
Somehow, nilling a certain value ends up updating the statistic. So if you do something like this:
Lua:
l = {11,22,33,44,55}
l[3] = nil
x = l[5]
l[5] = nil
print(#l) > statistics updated: now returns 2
l[5] = x > (returning to prior value)
I'm a little bit stuck here, because if the table is not creating like l = {11,22,33,44,55}, but instead like this:
l = {}
for x=1,5 do l[x]=x*11 end
Now suddenly not nilling element 5, but element 4 updates the statistics.
Meanwhile I'm experimenting a bit, but could someone with insight in lua chime in, and help me find the most efficient way to find a non-nil value in a straight array