Using Director > Writing Scripts with Lingo > Using lists > Copying lists |
![]() ![]() ![]() |
Copying lists
Assigning a list to a variable and then assigning that variable to a second variable does not make a separate copy of the list. For example, the statement landList = ["Asia", "Africa"]
creates a list that contains the names of two continents. The statement continentList = landList
assigns the same list to the variable continentList
. However, adding Australia to landList
using the statement add landList, "Australia"
automatically adds Australia to continentList
also. This happens because both variable names point to the same object in memory.
To create a copy of a list that is independent of the first list:
Use the duplicate()
function. See duplicate() (list function)
.
For example, this statement creates a list and assigns it to the variable oldList
:
oldList = ["a", "b", "c"]
This statement uses the duplicate()
function to make an independent copy of the list and assign it to the variable newList
:
newList = duplicate(oldList)
After newList
is created, editing either oldList
or newList
has no effect on the other.
![]() ![]() ![]() |