Name
CopyTable -- make independent copy of a table (V4.6)
Synopsis
t = CopyTable(src[, shallow])
Function
This function can be used to make an independent copy of the specified source table. As you have probably noticed, when assigning a table to a new variable using the equal (=) operator, only a reference to the table will be assigned to the new variable. This is done due to efficiency reasons because making complete copies of the table is not necessary in most cases. In some cases, however, you need to have a fully independent copy of the table. This can be done using this function.

Starting with Hollywood 6.0 this function accepts an optional argument named shallow. If you set this argument to True, CopyTable() will do a shallow copy of the table which means that instead of making an independent copy, all sub-tables will only be copied by reference. A copy by reference means that if the source table is modified, all copies by reference will be modified as well. Shallow copies of a table have the advantage that they save resources and they can also come in handy in case of self-referential tables which would lead to a stack overflow during a deep copy.

See Tables for details.

Inputs
src
table to copy
shallow
optional: specifies whether or not a shallow copy of the table shall be made (defaults to False) (V6.0)
Results
t
deep or shallow copy of table
Example
t1 = {1, 2, 3, 4, 5}
t2 = t1
t2[0] = 10
DebugPrint(t1[0]) ; -> prints 10 because t2 is only a reference to t1
t3 = CopyTable(t1)
t3[0] = 20
DebugPrint(t1[0]) ; -> prints 10 now!
This code demonstrates first the copy-by-reference default behaviour of Hollywood which only creates a reference to an existing table. Afterwards, a deep copy is made using CopyTable().

Show TOC