如何在VBScript中删除Dictionary中的重复值

时间:2014-07-11 13:24:34

标签: vbscript

  

从字典中删除重复值

Dim obj_ref
Set obj_ref = CreateObject("scripting.dictionary")
obj_ref.CompareMode = vbTextCompare
obj_ref.Add "one","JAVA"
obj_ref.Add "two",".NET"
obj_ref.Add "three","C"
obj_ref.Add "four","C++"
obj_ref.Add "five","JAVA"
obj_ref.Add "six","C"
obj_ref.Add "sev","C++"

1 个答案:

答案 0 :(得分:2)

键在词典中必须是唯一的。因此,如果您尝试创建一组唯一值,请将它们指定为字典的键。基本上,在您的方案中,只需交换密钥名称和值。

Dim d
Set d = CreateObject("Scripting.Dictionary")

If Not d.Exists("JAVA") Then d.Add "JAVA", "one"   ' Will get added
If Not d.Exists("NET")  Then d.Add "NET",  "two"   ' Will get added
If Not d.Exists("C")    Then d.Add "C",    "three" ' Will get added
If Not d.Exists("C++")  Then d.Add "C++",  "four"  ' Will get added
If Not d.Exists("JAVA") Then d.Add "JAVA", "five"  ' Will NOT get added.
If Not d.Exists("C")    Then d.Add "C",    "six"   ' Will NOT get added.
If Not d.Exists("C++")  Then d.Add "C++",  "sev"   ' Will NOT get added.

现在,您可以测试以查看密钥是否已存在于字典中,并仅在尚不存在时添加密钥。