Dictionary Object

Microsoft® Windows® 2000 Scripting Guide

Scripts often retrieve information from an outside source, such as a text file or a database. After this information has been retrieved, it needs to be stored in memory so that the script can act upon it. Information such as this can be stored in individual variables (one variable for each bit of information) or in an array. Alternatively, information can also be stored in a Dictionary object.

The Dictionary object functions as an associative array; that is, it stores values in key-item pairs. This is different from an array, which uses a numeric index to store values. For example, a one-dimensional array consisting of state capitals might look like this:

  • 0 - Olympia

  • 1 - Salem

  • 2 - Sacramento

By contrast, a Dictionary containing state capitals might look like this:

  • Washington - Olympia

  • Oregon - Salem

  • California - Sacramento

If your command-line arguments consist of single-item entries, such as computer names, you can use an array to hold the item names. However, the Dictionary object does offer several advantages over arrays. In particular, the Dictionary object does not require a script to:

  • Specify (or modify) the number of elements being stored. With an array, you must specify the size in advance, or repeatedly resize the array as new elements are added.

  • Know the index number assigned to a particular element. Any element in a Dictionary can be accessed either by key or by item.

This makes Dictionaries an ideal tool for system administration scripts that need to retrieve information such as server names from an outside source and then store that information in memory for later use.