lua-users home
lua-l archive

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



I remember this thread passing by a couple of months ago, but I did not understand then, and I still don't understand now why I can't go like this:

    local t = { a = 1, b = "hello" }
    for p in t do
        print("t[",p,"] = ",t[p])
    end

This works in 5.0 and I use it extensively. Syntactically, it looks very nice to me, much nicer than table.foreach() or using pairs(). I know that pairs has the nice feature that it gives me back the already dereffed value. But I really dislike the inscrutability of this method. Also, it seems like it would be more efficient to use a table directly for a couple of reasons. First, the special code that must have been in place to handle a table directly would be faster than having to make a function call every time. And if I just wanted the property and not the value, pairs() will do a deref that I don't need. Admittedly in this case I was dereffing the property anyway. But in cases where I don't need to, seems like I don't have a way to do that anymore!

What problem was being caused by "for p in t do" ???

--
chris marrin              ,""$, "As a general rule,don't solve puzzles
chris@marrin.com        b`    $  that open portals to Hell" ,,.
        ,.`           ,b`    ,`                            , 1$'
     ,|`             mP    ,`                              :$$'     ,mm
   ,b"              b"   ,`            ,mm      m$$    ,m         ,`P$$
  m$`             ,b`  .` ,mm        ,'|$P   ,|"1$`  ,b$P       ,`  :$1
 b$`             ,$: :,`` |$$      ,`   $$` ,|` ,$$,,`"$$     .`    :$|
b$|            _m$`,:`    :$1   ,`     ,$Pm|`    `    :$$,..;"'     |$:
P$b,      _;b$$b$1"       |$$ ,`      ,$$"             ``'          $$
 ```"```'"    `"`         `""`        ""`                          ,P`