lua-users home
lua-l archive

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


John Belmonte wrote:
> Philippe Lhoste wrote:
> > Sorry if I don't answer correctly to your request, but I believe Lua
> already
> > have a mechanism for this.
> >
> > foreach (table, func)
> > foreachi (table, func)
>
> Sorry, I should have addressed these library functions in my
> post.  Well, if
> foreach() is so great, why do we have a special for construct to
> do the same
> thing?  Remember that the for command was created after these functions
> already existed.  I'll bet that since for loops were introduced
> as a way to
> iterate tables, most of us don't use foreach() anymore.  Having to create
> function objects can sometimes make code hard to follow.   So
> since there is
> a form of for matching foreach(), why not foreachi() too?
>
> For comparison, here is my example again using foreachi.  It's not bad in
> the case of a one-statement function, but for larger functions it becomes
> less readable that using a for construct.
>
>     foreachi(list, function(i, val) print(val) end)

OK. But actually, it wasn't even these functions I was thinking of, it was
the construct:
>>>
The table for statement traverses all pairs (index,value) of a given table.
It has the following syntax:

       stat ::= for name `,' name in exp1 do block end

A for statement like
       for index, value in exp do block end
<<<

I am probably thick (still a newbie here, lurks a lot, doesn't codes much
Lua yet), but how this differ deeply from your:
>>>
    for val in list do
        print(val)
    end
<<<
?
I feel I am missing something here, as I am alone to ask this, but you seems
patient with lusers... (a word I learned here :-)
Perhaps your lists are not tables?

Regards.

--._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.--
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/
--´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`--