lua-users home
lua-l archive

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


Tables are passed by reference, so the assignment to cloneset dies NOT copy the set ... So really the table.remove is working durectly on the original 'a' set ... Hence the result

--Tim

On Mar 30, 2013, at 9:27 AM, Mason Mackaman <masondeanm@aol.com> wrote:

> sorry, I realized right after I sent the message I forgot to include what version I was working with. I've confirmed this happening with 5.1.4 and 5.2.0.
> 
> Hello everyone, I'm pretty new to programming and even newer to Lua, so please humor me. I feel I've found a bug but it's most likely just me being a noob.
> 
> 
> function f(set)
>    local newset,cloneset={},set
>    for x=1,#cloneset do
>        newset[x]=cloneset[#cloneset]
>        table.remove(cloneset)
>    end
>    return newset
> end
> function
> 
> g(set)
>    set=f(set)
> end
> a={1,2,3,4,5,6,7,8,9}
> g(a)
> print(#a)
> 
> 
> This is the simplest form I can put the bug in. I feel like it should output 9 but instead it outputs 0. If someone could tell my why this is happening I would be much obliged.
> 
> info:
> - #f(a)=9
> - every time table.remove(cloneset) is executed it also executes the equivalent of table.remove(a)
> 
> 
>