lua-users home
lua-l archive

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


2009/7/28 Jerome Vuarand <jerome.vuarand@gmail.com>:

> The array is not such a fundamental data structure in Lua, as opposed
> to C for example. I think all these pseudo-problems around them
> quickly vanish once you start programming in Lua "à la Lua".

Yes, I think a "native" Lua programmer will avoid using ipairs and the
# operator on arrays unless they know the arrays won't have holes.
Dealing with embedded nils can be a little fiddly but there are
performance advantages in not having to traverse the array after each
element is added (in the case of the # operator for example).

2009/7/25 Cosmin Apreutesei <cosmin.apreutesei@gmail.com>:

> so is ipairs() really needed?

Not as much as pairs() is needed. To me it's almost in the category of
syntactic sugar, as the keys to an array without holes are accessible
with a simple arithmetical loop:

t = { "one", "two", "three" }
for i = 1, #t do
    ...
end
-- or this
local i = 1
while t[i] do
    ... -- though this requires an extra table lookup per iteration
    i = i + 1
end

Vaughan