How do I clone a table

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

How do I clone a table

Post by Ranguna259 »

I'd like to know a way to clone tables.
Whenver I try to do

Code: Select all

table2=table1
and change data inside table 2, that same data will change in table1.

I've thought of using serialization libs to serialize a table and then load the string into the new table but mayber there's another way ..?
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
jangsy5
Prole
Posts: 3
Joined: Tue Apr 15, 2014 6:35 am

Re: How do I clone a table

Post by jangsy5 »

Code: Select all

a = {1,2,3}
b = {}

for k, v in ipairs(a) do
    b[k] = v
end
Pretty much means instead of b referring to a, b has a table that refers to the same values as a. Which is kind of cloning right? Well this is the best I can think of. You have to note that it'll reference values that are tables so you need to clone those too, probably using if statement to check.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: How do I clone a table

Post by davisdude »

*Made by Robin

Code: Select all

function DeepCopy( Table, Cache ) -- Makes a deep copy of a table. 
    if type( Table ) ~= 'table' then
        return Table
    end

    Cache = Cache or {}
    if Cache[Table] then
        return Cache[Table]
    end

    local New = {}
    Cache[Table] = New
    for Key, Value in pairs( Table ) do
        New[DeepCopy( Key, Cache)] = DeepCopy( Value, Cache )
    end

    return New
end
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: How do I clone a table

Post by Ranguna259 »

jangsy5 wrote:You have to note that it'll reference values that are tables so you need to clone those too, probably using if statement to check.
I'd already thought of that but, as you said, tables within tables are the problem and if checks would not be the solution, but apparently Robin found a way.(or someone else did because I think I've seen his solution somewhere else)
davisdude wrote:*Made by Robin

Code: Select all

function DeepCopy( Table, Cache ) -- Makes a deep copy of a table. 
    if type( Table ) ~= 'table' then
        return Table
    end

    Cache = Cache or {}
    if Cache[Table] then
        return Cache[Table]
    end

    local New = {}
    Cache[Table] = New
    for Key, Value in pairs( Table ) do
        New[DeepCopy( Key, Cache)] = DeepCopy( Value, Cache )
    end

    return New
end
Thank you very much for sharing, and thank you Robin for coding this :)
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 8 guests