lua-users home
lua-l archive

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

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
leaves you with this annoying problem:

   for k,v in l do
     -- do something which assumes k is a number

> 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]

Is that uglier than
   for k, v in l using numerical_order do
      -- do something with k and v

I personally like the syntax I illustrate in
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
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.)

	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
aforementioned page.

You need the Do function because Lua doesn't allow the evaluation of
for side-effects, except for function calls. You could just as well use
assignment statement:

	local _ = print^ l

but I find the Do syntax less ugly.