Thread Tools Display Modes
05-17-06, 05:07 PM   #1
Aqualyt
A Defias Bandit
Join Date: Apr 2006
Posts: 3
Duplicating a table?

Code:
a = {};
a[key1] = {};
a[key2] = {};

b = {};
How would I make b a duplate of a?
  Reply With Quote
05-17-06, 06:29 PM   #2
Iriel
Super Moderator
WoWInterface Super Mod
Featured
Join Date: Jun 2005
Posts: 578
Assuming you're dealing with a simple 'shallow copy' (i.e. not deeply nested tables that also have to be copied)

b = {};
for k,v in pairs(a) do b[k] = v end
  Reply With Quote
05-17-06, 10:14 PM   #3
Aqualyt
A Defias Bandit
Join Date: Apr 2006
Posts: 3
Thanks!
  Reply With Quote
05-22-06, 06:23 PM   #4
Dhargo
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Apr 2005
Posts: 32
if you need a true copy (no pointer references to the other table's elements)

Code:
function tablecopy(tbl)
  if type(tbl) ~= "table" then return tbl end
  local t = {}
  for i,v in pairs(tbl) do
    t[i] = tablecopy(v)
  end
  return t
end
I think that's right, but I wrote it while killing the last few minutes of a very long day
  Reply With Quote
05-23-06, 01:08 AM   #5
ckknight
A Warpwood Thunder Caller
 
ckknight's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 90
without metatables:

Code:
function tablecopy(tbl)
  if type(tbl) ~= "table" then return tbl end
  local t = {}
  for i,v in pairs(tbl) do
    t[i] = tablecopy(v)
  end
  table.setn(t, table.getn(tbl))
  return t
end
with metatables (shallow copy):

Code:
function tablecopy(tbl)
  if type(tbl) ~= "table" then return tbl end
  local t = {}
  for i,v in pairs(tbl) do
    t[i] = tablecopy(v)
  end
  table.setn(t, table.getn(tbl))
  return setmetatable(t, getmetatable(tbl))
end
with metatables (deep copy):

Code:
function tablecopy(tbl)
  if type(tbl) ~= "table" then return tbl end
  local t = {}
  for i,v in pairs(tbl) do
    t[i] = tablecopy(v)
  end
  table.setn(t, table.getn(tbl))
  return setmetatable(t, tablecopy(getmetatable(tbl)))
end
I think. the main difference from Dhargo's is the setn and the metatables. You'll probably just want the shallow metatable copy.
  Reply With Quote
05-23-06, 11:25 AM   #6
Iriel
Super Moderator
WoWInterface Super Mod
Featured
Join Date: Jun 2005
Posts: 578
The challenges with deep copies is knowing what to, and what not to copy, as well as properly handling self referential structures.

Quite often you dont want to deep copy metatable entries (though perhaps you want to shallow copy the metatable).

You usually dont want to copy tables-as-keys, but sometimes it's applicable.

Any self respecting deep copy method probably wants to maintain a temporary mapping table to avoid infinite recursion when it encounters a self-referential table. Since we can use tables as table keys, it's remarkably easy in lua to reconstruct such copies.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Duplicating a table?


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off