Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I clone a Dictionary object?

I have a Dictionary object in VBScript. How can I copy all the objects contained in it to a new Dictionary, i.e. create a clone/duplicate of the dictionary?

like image 720
Vineel Kumar Reddy Avatar asked Jun 11 '10 11:06

Vineel Kumar Reddy


People also ask

How do you clone a dictionary?

to add the div. Then we can clone the div by writing: const div = document. getElementById('foo') const clone = div.

How do you clone a dictionary in Python?

Use copy() This is a built-in Python function that can be used to create a shallow copy of a dictionary. This function takes no arguments and returns a shallow copy of the dictionary. When a change is made to the shallow copy, the original dictionary will remain unchanged.

How do I copy a dictionary key?

By using dict. copy() method we can copies the key-value in a original dictionary to another new dictionary and it will return a shallow copy of the given dictionary and it also helps the user to copy each and every element from the original dictionary.

How do I make a deep copy of a dictionary?

deepcopy() To make a deep copy, use the deepcopy() function of the copy module. In a deep copy, copies are inserted instead of references to objects, so changing one does not change the other. The following is an example of applying the deepcopy() function to a slice.


1 Answers

Create a new Dictionary object, iterate through the keys in the original dictionary and adds these keys and the corresponding values to the new dictionary, like this:

Function CloneDictionary(Dict)
  Dim newDict
  Set newDict = CreateObject("Scripting.Dictionary")

  For Each key in Dict.Keys
    newDict.Add key, Dict(key)
  Next
  newDict.CompareMode = Dict.CompareMode

  Set CloneDictionary = newDict
End Function

This should be enough in most cases. However, if your original dictionary holds objects, you'll have to implement deep cloning, that is, clone these objects as well.

like image 101
Helen Avatar answered Sep 23 '22 13:09

Helen