VBScript - Dictionary Object
The Dictionary object stores name/value pairs (referred to as the key and item respectively) in an array. The key is a unique identifier for the corresponding item and cannot be used for any other item in the same Dictionary object.
The following code creates a Dictionary object called "cars", adds some key/item pairs, retrieves the item value for the key 'b' using the Item property and then outputs the resulting string to the browser.
Code:
Output:
"The value corresponding to the key 'b' is Buick"
PROPERTIES
CompareMode Property
The CompareMode property is used to set and return the key's string comparison mode which determines how keys are matched while looking up or searching.
Syntax: object.CompareMode[ = comparison_mode]
The CompareMode property is used to set and return the key's string comparison mode which determines how keys are matched while looking up or searching. Keys can be treated as case-sensitive or case-insensitive.
To do the comparison, you use the Comparison Constants. You may use either the CONSTANT (left column) or the VALUE (center column) in your code and get the same results.
CONSTANT VALUE DESCRIPTION
VBBinaryCompare 0 binary Comparison
VBTextCompare 1 Text Comparison
VBDataBaseCompare 2 Compare information inside database
In the following example the Add method fails on the last line because the key "b" already exists. If CompareMode were set to VBBinaryCompare a new key "B" (upper case) with a value of "Bentley" will be added to the dictionary.
Code:
Output:
"Alvis"
"Buick"
"Cadillac"
Count Property
The Count property is used to determine the number of key/item pairs in the Dictionary object.
Syntax: object.Count
Item Property
The Item property allows us to retreive the value of an item in the collection designated by the specified key argument and also to set that value by using itemvalue.
Syntax: object.Item(key) [ = itemvalue]
Key Property
The Key property lets us change the key value of an existing key/item pair.
Syntax: object.Key(keyvalue) = newkeyvalue
The Item property sets or returns the value of an item for the specified key and Dictionary.
Keyvalue is the value of the key associated with the item being returned or set and itemvalue is an optional value which sets the value of the item associated with that key.
If the specified key is not found when attempting to change an item, a new key/item pair is added to the dictionary. If the key is not found while trying to return an existing item, a new key is added and the item value is left empty.
Code:
Output:
"Value associated with key 'c' has changed to Corvette"
METHODS
Add Method
The Add method is used to add a new key/item pair to a Dictionary object.
Syntax: object. Addkeyvalue, itemvalue
Exists Method
The Exists method is used to determine whether a key already exists in the specified Dictionary. Returns True if it does and False otherwise.
Syntax: object.Exists(keyvalue)
Items Method
The Items method is used to retreive all of the items in a particular Dictionary object and store them in an array.
Syntax: [arrayname = ]object.Items
Keys Method
The Keys method is used to retreive all of the keys in a particular Dictionary object and store them in an array.
Syntax: [arrayname = ]object.Keys
Remove Method
The Remove method is used to remove a single key/item pair from the specified Dictionary object.
Syntax: object. Remove(keyvalue)
RemoveAll Method
The RemoveAll method is used to remove all the key/item pairs from the specified Dictionary object.
Syntax: object.RemoveAll
Example
Add an Item to Dictionary
Set oDictionary = CreateObject("Scripting.Dictionary")
oDictionary.Add "a","Akhila" 'Adds a item and key pair
oDictionary.Add "b","Bhanu"
oDictionary.Add "d","Deepthi"
MsgBox "The number of key/item pairs "&oDictionary.Count
For Each obj in oDictionary
msgbox "The key for "&obj&" and the associated data is "&oDictionary(obj)
Next
Msgbox "The value of key a is "&oDictionary.Item("a")
Determine if a specified key exists in the Dictionary Object
Set oDictionary = CreateObject("Scripting.Dictionary")
oDictionary.Add "a","Akhila" 'Adds a item and key pair
oDictionary.Add "b","Bhanu"
if oDictionary.Exists("b") Then msgbox "Key exists in the Dictionary"
Remove a key/item pair from Dictionary Object
Set oDictionary = CreateObject("Scripting.Dictionary")
oDictionary.Add "a","Akhila" 'Adds a item and key pair
oDictionary.Add "b","Bhanu"
oDictionary.Add "d","Deepthi"
oDictionary.Remove("b")
Removes all key/item pairs from Dictionary Object
Set oDictionary = CreateObject("Scripting.Dictionary")
oDictionary.Add "a","Akhila" 'Adds a item and key pair
oDictionary.Add "b","Bhanu"
oDictionary.Add "d","Deepthi"
oDictionary.RemoveAll
VBScript: comparing two files using dictionary object
Here is a VBScript function to compare 2 files and get the changes between them. The function will return a multi-line string with what was added, modified and deleted between the old and new file.
This function was initially developed for me to compare changes between an old and new input file in my script. I need to know which server was added or removed and which server had its parameters modified. The input files is a delimited file using “~” as a delimiter and contains 2 values in each line: “server name” & “parameter”.
This will help explain why the code was written the way it is. You can use this and change according to what you require. Some of you may cringe that the liberal use of single letter variables, I have my own thoughts about this on the readability versus efficiently in short programming codes
Function Get_FileDiff(pFileOld, pFileNew)
dim fso, t, txt
dim d1, d2, i, k
Get_FileDiff = ""
Set fso = CreateObject("Scripting.FileSystemObject")
if fso.FileExists(pFileOld) and fso.FileExists(pFileNew) then
Set d1 = CreateObject("Scripting.Dictionary") 'old file contents
Set d2 = CreateObject("Scripting.Dictionary") 'new file contents
set txt = fso.OpenTextFile(pFileOld, 1)
do while not txt.AtEndOfStream
t = split(ucase(txt.readline),"~")
d1.Add t(0), t(1)
loop
txt.close
set txt = fso.OpenTextFile(pFileNew, 1)
do while not txt.AtEndOfStream
t = split(ucase(txt.readline),"~")
d2.Add t(0), t(1)
loop
txt.close
k = d1.keys
for i = 0 To d1.Count -1 ' Iterate the array.
if not d2.Exists(k(i)) then
Get_FileDiff = Get_FileDiff & "deleted: " & k(i) & vbCrLf
else
if d1(k(i)) <> d2(k(i)) then
Get_FileDiff = Get_FileDiff & "modified: " & k(i) & ": old=" & d1(k(i)) & ",new=" & d2(k(i)) & vbCrLf
end if
end if
next
k = d2.keys
for i = 0 To d2.Count -1 ' Iterate the array.
if not d1.Exists(k(i)) then Get_FileDiff = Get_FileDiff & "added: " & k(i) & vbCrLf
next
else
Get_FileDiff = "error: One of the files: " & pFileOld & " or " & pFileNew & " not found!"
end if
set fso = Nothing
End Function
Automation Infotech
Tuesday, July 26, 2011
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment