lua-users home
lua-l archive

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


On Tuesday 28 December 2010 16:15:39 Mark Hamburg wrote:
> On Dec 28, 2010, at 11:45 AM, Richard Hundt wrote:
> > Is there something inherently Bad about doing this?
> >
> > for k,v in next, t, #t > 0 and #t or nil do
> >   ...
> > end
> 
> There is nothing in the contract for next that guarantees that it will
>  work.

Can somebody please explain how Mark's algorithm works? In the docs at 
http://www.lua.org/manual/5.1/manual.html it lists the following syntax for 
next():

next (table [, index])

It states that the return is a key and a value. 

I see no resemblance between that and:

for k,v in next, t, #t > 0 and #t or nil do
   ...
end

And the same documentation shows the generic for statement as:

for var_1, ···, var_n in explist do block end

explist is supposed to be an iterator function. I have no idea how "next, t, 
#t > 0 and #t or nil" evaluates to an iterator function.

I don't get it. And yet Mark's syntax works at least on my test program. Could 
somebody please explain what Mark's syntax is doing?

Thanks

SteveT

Steve Litt
Recession Relief Package
http://www.recession-relief.US
Twitter: http://www.twitter.com/stevelitt