One of the nice features of other scripting languages, such as Perl and LISP, is what is called an associative array. An associative array differs from a “normal” array in one major way: rather than being indexed numerically (i.e. 0, 1, 2, 3, …), it is indexed by a key, or an English-like word. VBScript has something very similar to an associative array. This object is called the Dictionary object. Dictionaries are part of Microsoft's Script Runtime Library.
To construct an instance of a dictionary object, just use the following lines of code:
Dim objDictionary Set objDictionary = CreateObject("Scripting.Dictionary")
It's that simple. To add items to your dictionary object, all you need to do is use the Add method. The Add method takes two parameters, the key and the item. The key is similar to the index in a numerically-based, indexed array, and the item is the value at that index. Here is an example of using the add method:
objDictionary.Add "Name", "Scott" objDictionary.Add "Age", "20"
Note that “Name” and “Age” are the keys and “Scott” and “20” are the items corresponding to those keys. To see if a key exists, just use the Exists method:
If objDictionary.Exists("Name") Then ' Do something Else ' Do something else End If
You can retrieve all of the items and keys using one of two methods. The Items method returns an array of all the items in the Dictionary object, and the Keys method returns an array of all the Keys in the Dictionary object.
Dim ItemsArray, KeysArray ItemsArray = objDictionary.Items KeysArray = objDictionary.Keys ' The # of Items = UBound(ItemsArray) ' You can traverse through the array to print out all the values
To get a specific item, use the Item property.
Dim MyName MyName = objDictionary.Item("Name") ' MyName = "Scott"
You can use the Count property to get the number of keys / items in a Dictionary object.
intCount = objDictionary.Count
The last important methods are the Remove and RemoveAll methods. RemoveAll removes all of the key / index pairs, while the Remove takes a Key as a parameter and removes the key/item pair.
objDictionary.Remove("Name") ' Now the "Name" / "Scott" are no more objDictionary.RemoveAll ' Now the entire Dictionary object is empty
On occasion, it may be important to sort your dictionary. You can sort a dictionary by using the following function
' Description: ' Sorts a dictionary by either key or item ' Parameters: ' objDict - the dictionary to sort ' intSort - the field to sort (1=key, 2=item) ' Returns: ' A dictionary sorted by intSort ' Function SortDictionary(objDict, intSort) ' declare constants Const dictKey = 1 Const dictItem = 2 ' declare our variables Dim strDict() Dim objKey Dim strKey,strItem Dim X,Y,Z ' get the dictionary count Z = objDict.Count ' we need more than one item to warrant sorting If Z > 1 Then ' create an array to store dictionary information ReDim strDict(Z,2) X = 0 ' populate the string array For Each objKey In objDict strDict(X,dictKey) = CStr(objKey) strDict(X,dictItem) = CStr(objDict(objKey)) X = X + 1 Next ' perform a a shell sort of the string array For X = 0 To (Z - 2) For Y = X To (Z - 1) If StrComp(strDict(X,intSort),strDict(Y,intSort),vbTextCompare) > 0 Then strKey = strDict(X,dictKey) strItem = strDict(X,dictItem) strDict(X,dictKey) = strDict(Y,dictKey) strDict(X,dictItem) = strDict(Y,dictItem) strDict(Y,dictKey) = strKey strDict(Y,dictItem) = strItem End If Next Next ' erase the contents of the dictionary object objDict.RemoveAll ' repopulate the dictionary with the sorted information For X = 0 To (Z - 1) objDict.Add strDict(X,dictKey), strDict(X,dictItem) Next End If End Function
The Dictionary object is not there to replace the array, but there are certainly times when it makes more sense to index your array using English-like terms as opposed to numerical values.
For more information on dictionaries, see Microsoft's Script Runtime reference.