vbscript的Scripting.Dictionary創(chuàng)建了類似于Key索引對(duì)應(yīng)Value值的字典對(duì)象,通過(guò)Key直接索引到指定的Value。
VBScript中Scripting.Dictionary使用示例如下:
復(fù)制代碼 代碼如下:
Dim objDict
Set objDict = WSH.CreateObject("Scripting.Dictionary")
' .Add(key, value)
objDict.Add "a", "value1"
objDict.Add "b", "value2"
objDict.Add "c", "value3"
'直接通過(guò)key=b索引到value2
WSH.Echo objDict.Item("b")
objDict.Remove "b" ' 刪除索引b及其對(duì)應(yīng)的值
' 以下是遍歷字典
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 ' 清空字典內(nèi)所有的key及其對(duì)應(yīng)value
Set objDict = Nothing