[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: tables holding nil: another way to look at the question itself
- From: Vaughan McAlley <ockegheim@...>
- Date: Wed, 29 Jul 2009 00:20:11 +1000
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