vbscript的Scripting.Dictionary創(chuàng)建了類似于Key索引對應(yīng)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及其對應(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及其對應(yīng)value
Set objDict = Nothing
更多信息請查看IT技術(shù)專欄