主頁(yè) > 知識(shí)庫(kù) > VBS基礎(chǔ)篇 - vbscript Dictionary對(duì)象

VBS基礎(chǔ)篇 - vbscript Dictionary對(duì)象

熱門標(biāo)簽:如何用機(jī)器人進(jìn)行電銷 除了地圖標(biāo)注還有這種生意嗎 神行者美術(shù)館地圖標(biāo)注 百度地圖標(biāo)注點(diǎn)距離代碼 佛山真人電銷機(jī)器人廠家 哪里有便宜的地圖標(biāo)注公司 地圖標(biāo)注政府哪個(gè)部門管 齊齊哈爾高德地圖標(biāo)注店 東營(yíng)快遞外呼系統(tǒng)

Dictionary是存儲(chǔ)數(shù)據(jù)鍵和項(xiàng)目對(duì)的對(duì)象,其主要屬性有Count、Item、Key,主要方法有Add、Exists、Items、Keys、Remove、RemoveAll。
創(chuàng)建Dictionary對(duì)象 

'定義并創(chuàng)建Dictionary對(duì)象,使用CreateObject創(chuàng)建并返回自動(dòng)化對(duì)象的引用
Dim Dic
Set Dic = CreateObject("Scripting.Dictionary")

添加鍵值 

Dim Dic
Set Dic = CreateObject("Scripting.Dictionary")
'向Dictionary對(duì)象中添加鍵值對(duì)
Dic.Add "Name", "Sirrah" 'Add方法第一個(gè)參數(shù)是Key值,第二個(gè)是Item值
Dic.Add "Age", 23  

刪除鍵值   

Dim Dic
Set Dic = CreateObject("Scripting.Dictionary")
Dic.Add "Name", "Sirrah" '向Dictionary對(duì)象中添加鍵值對(duì)
Dic.Add "Age", 23
Dic.Item("Age") = 22 '修改鍵Age的值
MsgBox Dic.Item("Age") '輸出22 

判斷鍵是否存在  

Dim Dic
Set Dic = CreateObject("Scripting.Dictionary")
Dic.Add "Name", "Sirrah" '向Dictionary對(duì)象中添加鍵值對(duì)
Dic.Add "Age", 23
MsgBox Dic.Exists("Age") '判斷鍵是否存在 

輸出所有鍵值
輸出Dictionary對(duì)象所有鍵值,這邊將介紹2種常用的循環(huán)方法,具體代碼如下:

Dim Dic,Dics
Set Dic = CreateObject("Scripting.Dictionary")
Dic.Add "Name", "Sirrah" '向Dictionary對(duì)象中添加鍵值對(duì)
Dic.Add "Age", 23
Dics = dic.Items 'Items返回一個(gè)包含所有Item值的數(shù)組
For i = 0 To dic.Count - 1 'Count返回Dictionary對(duì)象鍵數(shù)目
 str = str  Dics(i)  vbCrlf
Next
MsgBox(str)
Dim Dic,Dics
Set Dics = CreateObject("Scripting.Dictionary")
Dics.Add "Name", "Sirrah" '向Dictionary對(duì)象中添加鍵值對(duì)
Dics.Add "Age", 23
For Each Dic In Dics '循環(huán)遍歷Dictionary鍵,并輸出鍵值
 MsgBox Dics.Item(Dic)
Next

補(bǔ)充一個(gè)實(shí)例

腳本文件:a.vbs,包含字典的添加、刪除、判斷鍵是否存在、修改鍵、修改值、遍歷、統(tǒng)計(jì)鍵值對(duì)個(gè)數(shù)

'建立字典
Dim Dict : Set Dict = CreateObject("Scripting.Dictionary")

'添加鍵值對(duì)
Dict.Add "Key1", "Item1"
Dict.Add "Key2", "Item2"
Dict.Add "Key3", "Item3"

'字典中鍵值對(duì)數(shù)量
WScript.Echo "字典中現(xiàn)有鍵值對(duì)數(shù)量: "  Dict.Count '讓一個(gè)腳本在屏幕上顯示文本信息

WScript.Echo 

'檢查指定鍵是否存在
If Dict.Exists("Key1") Then
 WScript.Echo "Key1 存在!"
Else
 WScript.Echo "Key1 不存在!"
End If

If Dict.Exists("Keyn") Then
 WScript.Echo "Keyn 存在!"
Else
 WScript.Echo "Keyn 不存在!"
End If

WScript.Echo 

'遍歷字典
Sub TraverseDict
 Dim DictKeys, DictItems, Counter
 DictKeys = Dict.Keys
 DictItems = Dict.Items 'Items返回一個(gè)包含所有Item值的數(shù)組
 For Counter = 0 To Dict.Count - 1 'Count返回Dictionary對(duì)象鍵數(shù)目
 WScript.Echo _
  "鍵: "  DictKeys(Counter)  _ ' 字符串連接運(yùn)算符
  "值: "  DictItems(Counter)
 Next
End Sub

TraverseDict

WScript.Echo 

'在一個(gè)鍵值對(duì)中,修改鍵或修改值
Dict.Key("Key2") = "Keyx"
Dict.Item("Key1") = "Itemx"
TraverseDict

WScript.Echo 

'刪除指定鍵
Dict.Remove("Key3")
TraverseDict

WScript.Echo 

'刪除全部鍵
Dict.RemoveAll
WScript.Echo "字典中現(xiàn)有鍵值對(duì)數(shù)量: "  Dict.Count

調(diào)用方法:通過雙擊a.bat調(diào)用,a.bat代碼如下:

cscript a.vbs
pause

運(yùn)行結(jié)果截圖:

標(biāo)簽:???/a> 文山 邢臺(tái) 湖州 銅川 四平 鶴壁 西安

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《VBS基礎(chǔ)篇 - vbscript Dictionary對(duì)象》,本文關(guān)鍵詞  VBS,基礎(chǔ),篇,vbscript,Dictionary,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《VBS基礎(chǔ)篇 - vbscript Dictionary對(duì)象》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于VBS基礎(chǔ)篇 - vbscript Dictionary對(duì)象的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章