Subscribe to Windows IT Pro
July 09, 2003 12:00 AM

The Scripting Dictionary Makes It Easy

Look it up
Windows IT Pro
InstantDoc ID #39312
Rating: (0)
Downloads
39312.zip

Over the past few months, I've started seeing a less frequently used scripting object, Scripting::Dictionary, in scripts published in articles, books, and online. However, Windows Scripting Solutions readers who I've talked to say they rarely use the Scripting::Dictionary object in their scripts. Consequently, I thought I'd show you how the Dictionary object can make things a lot easier in certain types of scripts. "Understanding VBScript: The Dictionary Object—An Alternative to Arrays," June 2000, http://www.winscriptingsolutions.com, InstantDocID 8797, covers the basics, but I go into more depth.

Objects such as WScript, WshNetwork, and Scripting::Signer are Windows Script Host (WSH) objects; the Scripting::Dictionary object is one of two key objects that are part of the Microsoft Scripting Runtime Library (scrrun.dll). The other scrrun.dll object is the well-known Scripting::FileSystemObject. Given how frequently used this latter object is, it's strange that the former is so little known. Dictionary's obscurity is probably because of its name, which can be confusing to administrators without a development background.

A Dictionary object stores data items in an arraylike structure. However, in a Dictionary, the indexes to the data items don't have to be integers. A one-dimensional array in VBScript has the indexes 0, 1, 2, ... x, where x is the upper boundary of the array that UBound(array) dictates. A Dictionary object can have the indexes (known in Dictionary terminology as keys) a, b, and c or kitchen34, sink87, and hamster999.

The primary purpose of a Dictionary object is to create a collection of related information that you can then search or otherwise manipulate. A Dictionary has a hash (known in Perl as an associative array). Each data item in a Dictionary is associated with a key, and these two related items are known as key-value pairs. Many good reasons exist to use a Dictionary object instead of a one-dimensional array, but the main ones for me are that you don't need to call VBScript's ReDim function every time you want to add an item, the Dictionary object has many useful methods and properties that arrays don't have, you can remove items without leaving gaps in the middle of your data set, and you can use strings as keys.

Dictionary Methods and Properties
The Dictionary object has six methods and four properties, which you can read about at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/jsobjdictionary.asp?frame=true. To create a Dictionary object, you use the lines

Dim dicTest
Set dicTest = CreateObject _
  ("Scripting.Dictionary")

with the variable name of your choice. I often use the prefix dic in the variable name, but that's optional.

Now that you have a Dictionary object, you might want to add items to it. To do so, you use the Dictionary::Add method, which takes two arguments: the key and the value to which the key refers (i.e., the item). Here are some examples:

dicTest.Add "1", _
  "server43.europe.mycorp.com"
dicTest.Add "2", _
  "server82.oceania.mycorp.com"

dicTest.Add "servername", _
  "server43.europe.mycorp.com"
dicTest.Add "serverIP", _
  "10.123.123.123"

You could use all four lines in one script if you wanted to. In these examples, the keys are the first arguments (i.e., "1", "2", "servername", and "serverIP"), and the items are the second arguments.

If you try to use the Dictionary::Add method with an existing key, you'll get an error. However, you can use the Dictionary::Item property with an existing key to overwrite the key's old value with a new value, as in

dicTest.Item("servername") = _
  "server77.europe.mycorp.com"

To see whether a particular key is in a Dictionary, you can use the Dictionary::Exists method with the key as the argument, as in

If dicTest.Exists("servername") _
Then
   ' Key exists.
Else
   ' Key doesn't exist.
End If

As you can see, the Exists method performs a simple Boolean test.

Related Content:

ARTICLE TOOLS

Comments
  • Jim Hunter
    9 years ago
    Jul 16, 2003

    When running the following code from this article

    When running the following code from this article:



    25: While Not filInput1.AtEndOfStream

    26: strLine = filInput1.ReadLine

    27: dicData1.Add Split(strLine,",")(0), Split(strLine,",")(3)

    28: Wend



    I get the error: _05.vbs(27, 2) Microsoft VBScript runtime error: This key is already associated with an element of this collection. Any ideas on why I get this error? The line numbers were added by me after running code. I used a comma deliminated file with a .txt ending not .csv.


You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.