lua-users home
lua-l archive

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


It's mostly a badly designed api function, where the meaning of one argument completely changes if you add another argument.
Imo, it should be:
table.insert(t, value [, position])
or even better, split table.insert into table.append(t, value) and table.insert(t, position, value)


On Wed, Dec 8, 2010 at 10:27 AM, Dirk Laurie <dpl@sun.ac.za> wrote:
Here is a function that reads up to 10 numbers from a line:

function setup(line)
   local data = "">    while #data<10 do
       for w in line:gmatch("[^%s]+") do
           table.insert(data,(assert(tonumber(w),"Error")))
       for k=1,#data do print(k,data[k]) end
       break
       end
   end

E.g.
> setup("2 2 4 4 0 6 0 0")
1   2
2   2
3   4
4   4
5   0
6   6
7   0
8   0

Now, small change, remove the parentheses around the assertion.

           table.insert(data,assert(tonumber(w),"Error"))

Guess what happens before trying it!

Is this a newbie gotcha?

Dirk