我基本上尝试使用带有子词典的字典作为每个键的项目在vbscript中创建一个多维关联数组。
有效:
myAssocArray =
"app1" =
"appid" => "1"
"name" => "alpha"
"app2" =
"appid" => "2"
"name" => "beta"
这是我的代码。它循环遍历每个字典,但当它到达子字典时,它总是回显" last"元件。所以当回应" app1"它会显示" 2"和#34; beta"的名称但它应该显示" 1"和#34; alpha"分别
Dim dict
Dim dict2
Set dict = CreateObject("Scripting.Dictionary")
Set dict2 = CreateObject("Scripting.Dictionary")
' Create a dictionary to be used as the "items" value for the main dictionary
dict2.Add "appid", "1"
dict2.Add "name", "alpha"
' Add it to the main dictionary as the item with Key of "1"
dict.Add "app1", dict2
' Clear the temp second dictionary
dict2.RemoveAll
' Add a new dictionary dimension for the second item
dict2.Add "appid", "2"
dict2.Add "name", "beta"
dict.Add "app2", dict2
' Loop through the main dictionary, and go through each item (sub-dictionary)
For Each key In dict.Keys
MsgBox key
For Each key2 In dict.Item(key).Keys
MsgBox dict.Item(key).Item(key2)
Next
Next
打印出来
app1 =
"appid" = "2"
"name" = "beta"
app2 =
"appid" = "2"
"name" = "beta"
完全跳过第一项的值。知道为什么吗?
答案 0 :(得分:2)
dict.Add "app1", dict2
这会将引用添加到dict2
而不是副本,因此对dict2
的任何后续更改都会反映在"app1"
项目中。
(在您的脚本"app1"
末尾,"app2"
和dict2
都是相同的词典)
而不是RemoveAll
您需要一个新的词典,所以重复Set dict2 = CreateObject("Scripting.Dictionary")
答案 1 :(得分:0)
完整的脚本集
dict=CreateObject("Scripting.Dictionary")
'add items
dict.Add "app1", CreateObject("Scripting.Dictionary")
with dict("app1")
.Add "appid", "2"
.Add "val", "pepe"
end with
dict.Add "app2", CreateObject("Scripting.Dictionary")
with dict("app2")
.Add "appid", "4"
.Add "val", "maria"
end with
'traverse
For Each i In dict.keys
wscript.echo i
with dict(i)
For Each j In .keys
wscript.echo vbtab & j & vbtab & .item(j)
Next
end with
next