vbscript的Scripting.Dictionary創建了類似於Key索引對應Value值的字典對象,通過Key直接索引到指定的Value。
VBScript中Scripting.Dictionary使用示例如下:
復制代碼 代碼如下:
Dim objDict
Set objDict = WSH.CreateObject("Scripting.Dictionary")
' .Add(key, value)
objDict.Add "a", "value1"
objDict.Add "b", "value2"
objDict.Add "c", "value3"
'直接通過key=b索引到value2
WSH.Echo objDict.Item("b")
objDict.Remove "b" ' 刪除索引b及其對應的值
' 以下是遍歷字典
Dim objKeys, objItems, i
objKeys = objDict.Keys
objItems = objDict.Items
For i = 0 To objDict.Count -1
WSH.Echo "Key=" & objKeys(i) &_
" AND Value=" & objItems(i)
Next
' 判斷指定的key是否存在
If objDict.Exists("b") Then
WSH.Echo "Found it"
Else
WSH.Echo "Not Exists!"
End If
objDict.RemoveAll ' 清空字典內所有的key及其對應value
Set objDict = Nothing