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.


From: "Sam Roberts" <>
Sent: Wednesday, June 17, 2009 10:33 PM
To: "Lua list" <>
Subject: Re: Read only tables.

On Wed, Jun 17, 2009 at 12:16 PM, Imagine
Programming<> wrote:
I don't use table insert, i use the next local function plus the line below

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
  return n;
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.


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