The Dictionary is an important data-structure and is a very handy tool to store a collection of key-value pairs.
The Dictionary object can be created easily using Scripting language and is often popular on WSH (Windows Scripting Host) where the scripting language’s capability is often restricted. The COM object can be developed, created, and used in WSH.
The Dictionary object can be created using VBScript statement CreateObject, or using WScript.CreateObject if WScript object is available (e.g. executed using wscript.exe or cscript.exe)
In Javascript/JScript, new ActiveXObject can be used.
1 | var dict = new ActiveXObject("Scripting.Dictionary"); |
var dict = new ActiveXObject("Scripting.Dictionary");
We can use Add method to add a key pair (key, value) to the dictionary object.
1 2 3 4 5 | Set objDictionary = CreateObject("Scripting.Dictionary") objDictionary.Add "Printer 1", "Printing" objDictionary.Add "Printer 2", "Offline" objDictionary.Add "Printer 3", "Printing" |
Set objDictionary = CreateObject("Scripting.Dictionary") objDictionary.Add "Printer 1", "Printing" objDictionary.Add "Printer 2", "Offline" objDictionary.Add "Printer 3", "Printing"
We can access the property Count to get the number of items in the dictionary object.
1 | Wscript.Echo objDictionary.Count |
Wscript.Echo objDictionary.Count
We can use the method Exists to check if a key is in the dictionary.
1 2 3 4 5 | If objDictionary.Exists("Printer 4") Then Wscript.Echo "Printer 4 is in the Dictionary." Else Wscript.Echo "Printer 4 is not in the Dictionary." End If |
If objDictionary.Exists("Printer 4") Then Wscript.Echo "Printer 4 is in the Dictionary." Else Wscript.Echo "Printer 4 is not in the Dictionary." End If
The property Keys returns the key-array of the all items in the dictionary object.
1 2 3 4 5 | colKeys = objDictionary.Keys For Each strKey in colKeys Wscript.Echo strKey Next |
colKeys = objDictionary.Keys For Each strKey in colKeys Wscript.Echo strKey Next
We can use method Remove(key) to delete an item from the dictionary.
1 | objDictionary.Remove("Printer 2") |
objDictionary.Remove("Printer 2")
We can use method RemoveAll to delete everything.
1 | objDictionary.RemoveAll |
objDictionary.RemoveAll
–EOF (The Ultimate Computing & Technology Blog) —
loading...
Last Post: Codeforces: A. Circle Line
Next Post: Counting Squares