Lua - Table Cloning



Cloning a table refers to creating a copy of a table. Lua is not having any inbuilt standard library for cloning a table, but we can create this functionality easily. A table is a very versatile data structure and can contains nested tables as well. In this chapter, we'll cover following cases while cloning the tables.

  • A numerically indexed table

  • A associative key-value based table

  • A table with nested table

Cloning a numerically indexed table

Let's create a clone function which can utilize the unpack() method to copy table entries. unpack() method returns all the entries of a table as separate values.

main.lua

-- function to get clone of passed table
function clone(original)
  -- create a new table of all entries of original table
  copy = { table.unpack(org) }
  -- return the copy
  return copy
end

-- original table
originalTable = { 11, 22, 33, 44, 55, 66}

-- cloned table
clonedTable = clone(originalTable)

-- print first entry of cloned table
print(clonedTable[1])

Output

When the above code is built and executed, it produces the following result −

11

Shallow Cloning a table of key-value form

Let's create a function which will create a shallow clone of a table in associative form. This function will copy top level values and direct entries but not the nested tables.

main.lua

-- function to get shallow clone of passed table
function shallowClone(original)
   -- create an empty table   
   local copy = {}
   
   -- loop through all entries	  
   for key, value in pairs(original) do
      copy[key] = value
   end
   
   -- return the cloned copy
   return copy
end

-- original table
originalTable = { Mon="Monday", Tue="Tuesday", Wed="Wednesday", Thu="Thursday",
Fri="Friday", Sat="Saturday", Sun="Sunday" }

-- cloned table
clonedTable = shallowClone(originalTable)

-- print an entry of cloned table
print(clonedTable.Sun)

Output

When the above code is built and executed, it produces the following result −

Sunday

Deep Cloning a table of key-value form

Let's create a function which will create a deep clone of a table in associative form. This function will copy the nested tables as well by making recursive calls.

main.lua

-- function to get deep clone of passed table
function deepClone(original)
   local copy
   if orig_type == 'table' then
      -- create an empty table
      copy = {}
      -- loop through all entries	  
      for key, value in next, orig, nil do
         copy[deepClone(key)] = deepClone(value)
      end
      setmetatable(copy, deepClone(getmetatable(original)))
   else
      -- in case of number, string etc.
	  copy = original
   end
   -- return the cloned copy
   return copy
end

-- original table
studentDetails = {
	student = { name = "Robert", age = 12},
	address = { city = "HYD", pincode = 500031}
}

-- cloned table
clonedTable = deepClone(studentDetails)

-- print an entry of cloned table
print(clonedTable.student.name)

Output

When the above code is built and executed, it produces the following result −

Robert
Advertisements