[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Iternation syntax. Was Re: getn
- From: Rici Lake <rlake@...>
- Date: Mon, 27 Aug 2001 11:55:15 -0500
Jay Carlson wrote:
>
> Why not go all the way?
>
> n_entry = {} -- new tables are never ==, right?
>
> l = {1,2,3,nil,5}
> l[n_entry] = 5
Yep, that's a standard way of putting private data in tables. But it
still
leaves you with this annoying problem:
for k,v in l do
-- do something which assumes k is a number
end
> This still leaves Lua without syntax for ordered table iteration, and IMO
> makes things that are simple and pretty in other scripting languages ugly
> and annoying in Lua.
What's wrong with:
for i = 1, getn(l) do
-- do something with l[i]
end
Is that uglier than
for k, v in l using numerical_order do
-- do something with k and v
end
?
I personally like the syntax I illustrate in
<http://lua-users.org/wiki/VectorGlue>,
which uses functionals. I didn't give the illustration of simple
iteration over a
vector, but it should be clear how I do it... maybe I'll add it at some
point.
However, you can do simple iteration by using map and having the
function return
nil, which will return an almost empty table which you can then throw
away. (I know
that's not very efficient but it's not that bad. It's at least a way to
play around
with possible syntaxes.)
Eg:
function Do() end
Do(print^ l)
-- or, if you prefer: Do(l ^print)
I think that's pretty expressive, but there are some other examples in
the
aforementioned page.
You need the Do function because Lua doesn't allow the evaluation of
expressions
for side-effects, except for function calls. You could just as well use
an
assignment statement:
local _ = print^ l
but I find the Do syntax less ugly.
R.