• Subject: Re: pairs and ipairs
• From: Claudio Grondi <claudio.grondi@...>
• Date: Sun, 22 Oct 2023 11:34:30 +0200

Hmmm ...

I haven't been missing such a function yet and it will sure add a tiny bit to the complexity where the latter is against the main goals of Lua.

In my eyes it does not make much sense for the purpose of iterating over the table items to differentiate between keys which are integer numeric values and keys which are not integer numeric values for the purpose of iterating over all the keys.

Please consider following code:

```lua
Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
> t={[3]="three",["four"]=4,["five"]=5}
> for i,v in ipairs(t) do print(i,v) end
> t={[1]="one",[2]="two",[3]="three",["four"]=4,["five"]=5}
> for i,v in ipairs(t) do print(i,v) end
1    one
2    two
3    three
> t={[2]="two",[3]="three",["four"]=4,["five"]=5}
> for i,v in ipairs(t) do print(i,v) end
> t={[1]="one",[3]="three",["four"]=4,["five"]=5}
> for i,v in ipairs(t) do print(i,v) end
1    one
>
```
as you can see, `if not tonumber(k) then ... end`  will DIFFER in the outcome from in the provided cases of the table. Because for listing of the array values it is necessary that the keys start with `1` and that  there is a continuous sequence of them.

Claudio

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