1
我基本上試圖在vbscript中使用帶有子字典的字典作爲每個鍵的項目來創建多維關聯數組。VBScript中的多維字典總是獲取最後的值?
有效:
myAssocArray =
"app1" =
"appid" => "1"
"name" => "alpha"
"app2" =
"appid" => "2"
"name" => "beta"
這是我的代碼。它循環遍歷每個字典,但是當它到達子字典時,它總是回顯「last」元素的值。所以當回顯「app1」時,它會顯示appid「2」和「beta」的名字,但它應該分別顯示「1」和「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"
完全跳過第一項的值。任何想法爲什麼?
此外,在Windows上,如果你可以使用VBScript你也可以在腳本宿主中使用JScript,這顯然使得關聯數組非常簡單。 – 2014-10-06 15:42:09
感謝亞歷克斯...關於Jscript的一個很棒的觀點..我甚至沒有想過如何使用它。這會讓生活更簡單。 – Dss 2014-10-06 15:58:05
您也可以直接在'Add'語句中創建嵌套字典:'dict.Add「app1」,CreateObject(「Scripting.Dictionary」)',然後像下面這樣添加嵌套的鍵/值對:'dict(「 app1「)。添加」appid「,」2「'。 – 2014-10-06 16:42:28