r/lua • u/ArturJD96 • Jan 15 '26
Using ipairs() – antipattern? It cuts half of the table upon field nil assignment.
> a = {1,2,3,4}
> for k,v in pairs(a) do print(v) end
1
2
3
4
> a[2] = nil
> for k,v in ipairs(a) do print(v) end
1
> a = {1,nil,3,4}
> for k,v in ipairs(a) do print(v) end
1
>Commentary: *I know* about pairs() and ipairs(). But I would be _really_ expecting 1,nil,2,3 and not loosing half of my table because I reasigned something to nil.
Is it intended that every assignment after a[2] = nil must be transferred to the hash part? It is sooo confusing.
Am I arriving at the right conclusion that using ipairs() might be an antipattern and I should stick to pairs or numerical for loop? Unless I am _really reallyyy_ sure that I have a LIFO-like table..?
EDIT:
Highlight of some important points from the discussion:
- In context of lists, a true bad practice is this: a[i] = nil
There are two alternatives:
- – table.remove() — preserves order, but is sometimes NOT recommended: https://stackoverflow.com/a/53038524/21565668 and the docs themselves https://www.lua.org/pil/19.2.html: "not particularly efficient, as they must move elements up and down."
- – a[I]=a[#a] a[#a]=nil — this is hacky and doesn't preserve order.
14
u/hawhill Jan 15 '26
Yes, this is fully intended behaviour. ipairs() is not exactly an antipattern, I'd say. It's just fine when you treat your tables in a manner that fits - I'd say: as lists, where the index has no importance of its own. When you delete from such a "list", you'd use table.remove(), not t[i]=nil, so it isn't exactly "LIFO" either.
3
u/weather_isnt_real Jan 15 '26
They are different functions. `ipairs` explicitly ends the iteration when it encounters a nil value.
I highly recommend reading the relevant sections from PIL:
https://lua.org/pil/4.3.5.html
and for writing your own iterators:
https://lua.org/pil/7.html
2
u/blobules Jan 15 '26
An indexed table is just a list, so you can´t poke holes in the middle.
When you don´t care about element order, remove an element by moving the last element:
Instead of t[i]=nil, use t[i]=t[#t] t[#t]=nil
If order is important, table.remove() will move all elements.
So in then end, indexed table are useful, but only for specific tasks.
11
u/PhilipRoman Jan 15 '26
neither ipairs or
for i = 1, #a ...will work with sparse arrays.The values after nil are not necessarily transferred to the hash part (that is an implementation detail anyway), It's just not possible to implement the behavior you want efficiently without breaking many optimizations.
ipairsat least is guaranteed to stop at the firstnilelementA good alternative is using
falseto denote missing elements.1
u/ArturJD96 Jan 15 '26
One more question about the code above. Why the hash stays at 4 and not 1?
(code relates to the code above)
4
u/weregod Jan 15 '26
Using lists with holes is antipattern. If you have table with holes you can't use length operator or ipairs.
If you know max index in table you can use numeric for. If you don't have max index you can only use pairs().
3
u/likethevegetable Jan 15 '26
I don't find it confusing: given that you can index a table with an undefined key and it yields nil instead of an error, the first nil would be the way to determine the end of the array.
3
u/Isogash Jan 15 '26
Ipairs is fine, just use false instead of nil if you really want to have optional elements that you can still iterate.
I would suggest don't use lists with missing elements it doesn't normally make sense to, if the element is missing then just remove it from the list.
If your "list" is really a numeric map then yes, using ipairs would be incorrect and you should use pairs.
1
u/[deleted] Jan 18 '26
table.remove table.insert