lua-users home
lua-l archive

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


This code snippet works, but it seems a little complicated indeed.
foo = {};
foo.a = 1;
foo.b = 2

tab={}
for k,v in pairs(foo) do
tab[k] = v
end

On Thu, Jan 21, 2021 at 11:25 AM 孙世龙 sunshilong <sunshilong369@gmail.com> wrote:
>
> How to create a new table based on an already existed table (through
> Lua or C API)?
>
> This code snippet seems impossible to achieve this goal:
>
> foo = {};
> foo.a = 1;
> foo.b = 2;
>
> new = foo;
> print(new, foo)  --they both point to the same table!
>
> new.a = new.a+1;  --the value of foo.a has been modified too.
>                               --I hope the table named `new` to be a
> table which is independent from the table named `foo`.