Microsoft®
Visual Basic® Scripting Edition Dictionary Object |
| Language Reference |
|
Object that stores data key, item pairs.
A Dictionary object is the equivalent of a PERL associative array. Items, which can be any form of data, are stored in the array using the object.Add key, item method. Each item is associated with a unique key. The key is used to retrieve an individual item and is usually a integer or a string, but can be anything except an array.The following code illustrates how to create a Dictionary object:
Dim d 'Create a variable Set d = CreateObject("Scripting.Dictionary") d.Add "a", "Athens" 'Add some keys and items d.Add "b", "Belgrade" d.Add "c", "Cairo" ...The Item value will be returned if the object is referenced with the key value 'object.[Item](key)[ = newitem]:
Response.Write d("c") Response.Write d.Item("c")Will display:
CairoCairoIf key is not found when attempting to return an existing item, a new key is created and its corresponding item is left empty. To avoid creating a new key and blank item, use .Exists(key) to check.
if d.Exists("f") then Response.Write d("f")Keys are, by defaut, case sensitive. To use non-case sensitive keys, set the CompareMode object.CompareMode[ = compare] where compare is 0 (Binary), 1 (Text), or 2 (Database).
Keys can be replaced using object.Key(key) = newkey or removed using object.Remove(key) which also removes the Item, of course. object.RemoveAll removes all the Keys and Items from the dictionary.
The For each command can be used to enumerate through the dictionary elements.
For each x in d Response.Write "<OPTION VALUE=" & x & ">" & d(x) Nextwill produce a list of the keys:
<OPTION VALUE=a>Athens <OPTION VALUE=b>Belgrade <OPTION VALUE=c>CairoNote that this code works only because of the "<OPTION>" string being pre-pended to the ilterator. The ilterator is an object and must be converted to a string before use. Null keys and other strange values can cause problems which must me managed via on error or TypeName() checking. Another way to ilterate the dictionary is with object.Keys which returns a zero based Array containing all existing keys and object.Count which returns the number of key/item pairs in the object.
a = d.Keys 'Get the Keys For i = 0 To d.Count -1 'Iterate the array Print a(i) 'Print Key Print d(a(i)) 'Print Item for that Key NextIt is also possible to ilterate the Items in the dictionary with object.Items which Returns a zero based array containing all existing Items.
a = d.Items 'get the Items For i = 0 To d.Count -1 'Iterate the array Print a(i) 'Print Item Next
© 1996 by Microsoft Corporation.
file: /Techref/language/asp/VBS/VBSCRIPT/277.htm, 5KB, , updated: 2007/8/20 17:20, local time: 2024/12/29 08:16,
3.147.73.117:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://sxlist.com/TECHREF/language/asp/VBS/VBSCRIPT/277.htm"> Microsoft® Visual Basic® Scripting Edition </A> |
Did you find what you needed? |
Welcome to sxlist.com!sales, advertizing, & kind contributors just like you! Please don't rip/copy (here's why Copies of the site on CD are available at minimal cost. |
Welcome to sxlist.com! |
.