• Subject: Re: Why no for table-iterator like e. g. "hashpairs"?
• From: bil til <flyer31@...>
• Date: Fri, 1 Nov 2019 08:27:24 -0700 (MST)

```... this is a nasty example, as then the table contains nil...

Could we perhaps change the code slightly, to make my
point more clear:

t= {1, 2, 3, x=10000, y= 11111} -- just to have some nice table start
for i= 1, 1000 do
local n= math.random(1000)
t[n]= n
end

print( #t)

for i= 1, 10 do
print( i, t[i])
end

This produced the following output for #t and the table t in my case:
440
1       1
2       2
3       3
4       nil
5       5
6       6
7       7
8       8
9       nil
10      10

So this is a "really nasty table" now... it has nils distributed nicely in
its key-value range 1...1000, and #t just gives some sort of
"estimated length".

To confirm that 440 is at least "quite reasonable", you can
print the table range 335...445, then I got:
435     nil
436     nil
437     nil
438     438
439     439
440     440
441     nil
442     nil
443     443
444     nil
445     nil

... so this confirms, that by some "reaonably fast checking of #t"
(= avoiding time-eating linear search) 440 might be some
"reaonable value for #t".

... ok ...

but anyway:
here ALL the numbered keys appear nicely ordered if
I iterate with for using the pairs iterator:

I would be happy already concerning the first 440 ones,
but in fact you can test the complete table like this:

k_last= 0
for k, v in pairs(t) do
if type(k) == 'number' then
if k <= k_last then
print ("sorting error in 1..#t: " .. k)
end
k_last= k
end
end
print( k_last)

... and the result will be:
1000

... so this ran nicely up to 1000 without
any error message, all nicely sorted for the
number indices...

(but I am happy to restrict our discussion for the
range 1...#t
I am happy if we should agree,
that in this range FOR SURE the keys will arrive
in sorted order... and that these keys 1...#t
will appear, BEFORE any other keys / any hash
keys will appear...

this is really helpful to know definitely... this is NOT
an academic question without real practical output,
I hope you also agree in this.)

--
Sent from: http://lua.2524044.n2.nabble.com/Lua-l-f2524044.html

```