How to Use the VBScript Scripting Dictionary

Implementing Associative Arrays with VBScript Programming

© Mark Alexander Bain

Jul 22, 2009
How to Use the VBScript Scripting Dictionary, Mark Alexander Bain
VBScript does not have the associative array (or hash) data type. However, what it does have is the scripting dictionary. If anything, it's even better than a hash.

Associative arrays (commonly called hashes) are very useful data structures. They allow the contents of an array to be indexed by a string rather than a number, providing the programmer with an easy method of creating look up tables in their applications.

Some programming languages such as perl have associative arrays built into them (for more on this read Using Associative Arrays in Perl Programming) while others, such as VBScript, do not. However, the VBScript programmer need not be concerned. Instead of associative arrays they have the scripting dictionary. With a scripting dictionary the VBScript programmer has no need of an associative array.

Creating and Populating a VBScript Scripting Dictionary

The VBScript programmer creates a new dictionary by using the CreateObject method:

Set hash = CreateObject ("Scripting.Dictionary")

The programmer can then use the dictionary object's add method to populate it. This accepts a key word and value as inputs:

hash.add "yard", 3
hash.add "fathom", 6
hash.add "furlong", 660
hash.add "mile", 5280

The dictionary's "item" method can be used to do the same job:

hash.item("nautical mile") = "TBA"

Once the programmer has populated the dictionary then they can access and update its contents very easily.

Accessing the Contents of a VBScript Dictionary

A programmer uses the key words to access the contents of a VBScript dictionary by using its "item" method, for example:

key = "nautical mile"
wscript.echo key & " = " & hash.item(key) & " feet"

They can also use this method to update the dictionary's contents:

hash.item(key) = 6080
wscript.echo key & " = " & hash.item(key) & " feet"

In this example the script's user would see the value of "nautical mile" change from "TBA" to 6080 (as shown in figure 1 at the bottom of this article).

Checking for the Existence of a VBScript Dictionary Entry

If the programmer happens to access a non existent key then the dictionary will return an empty string. Therefore the programmer may wish to check if a key actually exists. This (unsurprisingly) is done by using the "Exists" method, for example:

key = "acre"
if hash.exists(key) then
wscript.echo key & " = " & hash.item(key) & " feet"
else
wscript.echo key & " is not in the dictionary"
end if

By doing this the programmer can ensure that they only use values from existing dictionary entries. Of course, there is one more issue. When is a nautical mile not a nautical mile?

The Case Sensitive Dictionary

It's important to know that the following will result in two entries being created, not one:

hash.item("nautical mile") = "TBA"
hash.item("Nautical mile") = "6080"

And so:

wscript.echo "Nautical Mile = " & hash.item("nautical mile") & " feet"

would return "TBA" and not 6080. That's because the dictionary is case sensitive and sees "nautical mile" as a separate key to "Nautical mile". The answer is to set the dictionary's "CompareMode":

hash.CompareMode = 1

This must be places immediately after the CreateObject statement and before any items are added. If that's done then the keys will no longer be case sensitive.

Calculating the Number of Key-Value Pairs in a VBScript Dictionary

The actual number of key-value pairs in a dictionary can be calculated by using the "count" method":

wscript.echo "There are " & hash.Count & " key-value pairs"

This can be used to iterate through the whole dictionary when used with two more of the dictionary's methods:

  • Keys - returns an array containing the key words
  • Items - returns an array containing the values

So these can be used in a number of ways, for example:

keys = hash.Keys
items = hash.Items
for i = 0 to hash.Count - 1
wscript.echo Keys(i) & " = " & Items(i)
next

or:

keys = hash.Keys
for i = 0 to hash.Count - 1
wscript.echo Keys(i) & " = " & hash.item(Keys(i))
next

Both of which will print out all of the key words and their associated values.

Removing Items

As well as adding and updating elements, the programmer can delete them. They do this by using either the "Remove" or the "RemoveAll" methods. The "Remove" methods deletes a single entry:

hash.Remove("nautical mile"")

or all of the items can be deleted:

hash.RemoveAll

And so even though the VBScript programmer does not have an actual associative array structure, they have the scripting dictionary, and this gives them complete control over how named variables are stored and modified with their application.


The copyright of the article How to Use the VBScript Scripting Dictionary in Windows Programming is owned by Mark Alexander Bain. Permission to republish How to Use the VBScript Scripting Dictionary in print or online must be granted by the author in writing.


How to Use the VBScript Scripting Dictionary, Mark Alexander Bain
Figure 1 Creating a VBScript Dictionary, Mark Alexander Bain
     


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo