lua-users home
lua-l archive

Re: Syntax and redundancy

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


El 30/08/2005, a las 18:22, Chris Marrin escribió:
I hate weighing in on things like this, but there are many more cases where Lua not only requires more keystroked, but is more arcane as well.
At risk of being beaten up endlessly, how is this done in Lua?
 struct list { struct list* next } list;
 list* l;
 ... fill the list ...
 for (list* p = l; p != 0; p = p->next) { ... }
How is that done in Lua? This is both trying to illustrate that C's for loop has nice flexibility, and trying to understand how you would do something like this in Lua! I would do it like this:
 local l = { }
 ... fill the list
 local p = l
 while p ~= nil do
 ...
 p = p.next
 end
Any nicer ways?
I propose the following:
local l = { }
... fill the list
for _,value in l do
 ...
end
Of course, you may also write:
local l = { }
... fill the list
table.foreachi(l, function(_,value) ... end)
=)
--
Adrian Perez
GPUL/CLUG member » http://gpul.org
Por los puntos #20 y #54.

Attachment: PGP.sig
Description: Mensaje firmado digitalmente


AltStyle によって変換されたページ (->オリジナル) /