lua-users home
lua-l archive

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


On Wed, Jun 17, 2009 at 12:16 PM, Imagine
Programming<contact@imagine-programming.com> wrote:
> I don't use table insert, i use the next local function plus the line below
> that.

Same problem, different library function. Your users can redefine
pairs() by mistake, causing your lua code to fail.

> local function c(t)
>   local n=0;
>   for i, v in pairs(t) do
>       n=i;
>   end
>   return n;
> end;
> tValues[c(tValues)+1]="bla";   -- I use lua 5.0

Why are you doing this? Even in 5.0?

pairs() isn't guaranteed to iterate a table in any particular order,
you need ipairs() for that, and table.getn() returns the same
information without iterating the whole table.

And the whole thing does the same as table.insert(), which existed in 5.0.

Cheers,
Sam


http://www.lua.org/manual/5.0/manual.html#5.1

"The order in which the indices are enumerated is not specified, even
for numeric indices. (To traverse a table in numeric order, use a
numerical for or the ipairs function.)"