Overwrite Data

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.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Overwrite Data

Post by Positive07 »

Yeah that is exactly what deepCopy does, your simpler shallow copy does the following

Code: Select all

t = {
    a = {
        b = "hello"
    }
}
for k,v in pairs(t) do
    x[k] = t[k]
end
x.a.b = "bye"
print(t.a.b) --"bye"
print(t==x) --"false"
print(t.a == x.a) --"true"
while deep copy

Code: Select all

t = {
    a = {
        b = "hello"
    }
}
x = deepCopy(t)
x.a.b = "bye"
print(t.a.b) --"hello"
print(t == x) --"false"
print (t.a == x.a) --"false"
I think that Robins code breaks when you do this though:

Code: Select all

t = {
    a = t
}
x = deepCopy(t)
Since it can't handle table loops, if you wanted to do this you would first have to detect the loops with parsing... and that is really hard... maybe there is a library to do this
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
khamarr3524
Prole
Posts: 41
Joined: Thu Sep 05, 2013 8:48 pm

Re: Overwrite Data

Post by khamarr3524 »

Right well, it should suffice for my usage. The data files for Units are static and formatted by hand. Nothing strange should be going on in there. I will simply use a recursion method. I'll do something like

Code: Select all

function copyTable(t)
  local tbl = {}
  for k, v in pairs(t) do
    if type(v) == "table" then
     tbl[k] = copyTable(v)
    end
    tbl[k] = v
  end
  return tbl
end
I'm assuming this would work just fine?
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Overwrite Data

Post by Positive07 »

Yeah indeed, it is really similar to deep copy
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
ivan
Party member
Posts: 1918
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Overwrite Data

Post by ivan »

Minor bug:

Code: Select all

    if type(v) == "table" then
     tbl[k] = copyTable(v)
    end
    tbl[k] = v -- overwrites the copied table reference
Should probably be

Code: Select all

    if type(v) == "table" then
     tbl[k] = copyTable(v)
    else
      tbl[k] = v
    end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Overwrite Data

Post by Robin »

Positive07 wrote: I think that Robins code breaks when you do this though:

Code: Select all

t = {
    a = t
}
x = deepCopy(t)
Since it can't handle table loops,
Actually, that's exactly the sort of thing my code fixed, compared to some more naive attempts at deep copying. ;)

(Also, that should really be something like t = {}; t.a = t, but yeah.)
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 15 guests