lua-users home
lua-l archive

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


Because I prefer this way of inserting stuff in a nummerical indexed table,
it has always worked for me.

That was not the issue in the beginning, neither was overwriting certain
lua functions. My issue was overwriting a specific table used by the script. If the users do this, they will lose all the data stored in it. I was trying to prevent this.

Now, i use the advice to give them a duplicate table to play with.

Thanks,
Bas

--------------------------------------------------
From: "Sam Roberts" <vieuxtech@gmail.com>
Sent: Wednesday, June 17, 2009 10:33 PM
To: "Lua list" <lua@bazar2.conectiva.com.br>
Subject: Re: Read only tables.

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.)"