[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Documenting Lua gotchas for newbies.
- From: HyperHacker <hyperhacker@...>
- Date: Wed, 8 Dec 2010 09:36:50 -0700
On Wed, Dec 8, 2010 at 02:44, Kristofer Karlsson
<kristofer.karlsson@gmail.com> wrote:
> 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
>>
>>
>
>
I've often found myself writing table.append instead of table.insert
and ultimately decided to make it an alias for readability's sake.
--
Sent from my toaster.
- References:
- Documenting Lua gotchas for newbies., Pierre-Yves Gérardy
- Re: Documenting Lua gotchas for newbies., Roberto Ierusalimschy
- Re: Documenting Lua gotchas for newbies., David Kastrup
- Re: Documenting Lua gotchas for newbies., Miles Bader
- Re: Documenting Lua gotchas for newbies., Pierre-Yves Gérardy
- Re: Documenting Lua gotchas for newbies., Miles Bader
- Re: Documenting Lua gotchas for newbies., Roberto Ierusalimschy
- Re: Documenting Lua gotchas for newbies., Lorenzo Donati
- Re: Documenting Lua gotchas for newbies., Dirk Laurie
- Re: Documenting Lua gotchas for newbies., Kristofer Karlsson