lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



I suggest that you consider and become aware of the fact that `ipairs()` is not there in Lua to make it possible to iterate over the array part of a table storing array-like entries and another kind of entries.

`ipairs()` is there in order to make it easy to use the table as a list. This way you achieve brevity and skip the need of supporting an additional data type like for example done in Python scripting where you have two different types (a dictionary and a list type) to provide the same functionality as Lua utilizing a genius concept wich delivers it with the single table type.


On 10/22/23 10:52, Hans van der Meer wrote:
This question might have come up earlier, but I have not seen it. Thus I dare to ask.

With ipairs() a table is walked through the array sequence in the table.
With pairs() all elements are visited while traversing the table, the sequence as well as the records.
However, sometimes I need to visit the record keys only, leaving the elements of the sequence out.
Of course I can do:
for k,v in pairs(t) do
if not tonumber(k) then ... end
end

But why isn't there a kpairs() for the record keys? I think it would make some programs easier, especially where the relevant pairs() function is passed to another function.

If I am wrong I will gladly hear it.

yours sincerely
dr. Hans van der Meer