Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

developer:scriptsamples:dictionary [2015/09/14] (current)
Line 1: Line 1:
 +====== Utilizing the Dictionary Object ======
 +> **Developer:​** [[developer:​rhinoscript|RhinoScript]]
 +> **Summary:​** //This article discusses using VBScript'​s Dictionary object in your [[developer:​rhinoscript|RhinoScript]] tools.//
 +
 +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:
 +
 +<code vb>
 +Dim objDictionary
 +Set objDictionary = CreateObject("​Scripting.Dictionary"​)
 +</​code>​
 +
 +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:
 +
 +<code vb>
 + ​objDictionary.Add "​Name",​ "​Scott"​
 + ​objDictionary.Add "​Age",​ "​20"​
 +</​code>​
 +
 +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:
 +
 +<code vb>
 +If objDictionary.Exists("​Name"​) Then
 +' Do something
 +Else
 +' Do something else 
 +End If
 +</​code>​
 +
 +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.
 +
 +<code vb>
 +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
 +</​code>​
 +
 +To get a specific item, use the Item property.
 +
 +<code vb>
 +Dim MyName
 +MyName = objDictionary.Item("​Name"​)
 +' MyName = "​Scott"​
 +</​code>​
 +
 +You can use the Count property to get the number of keys / items in a Dictionary object.
 +
 +<code vb>
 +intCount = objDictionary.Count
 +</​code>​
 +
 +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.
 +
 +<code vb>
 +objDictionary.Remove("​Name"​)
 +' Now the "​Name"​ / "​Scott"​ are no more
 +objDictionary.RemoveAll
 +' Now the entire Dictionary object is empty
 +</​code>​
 +
 +On occasion, it may be important to sort your dictionary. You can sort a dictionary by using the following function
 +
 +<code vb>
 + '​ 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
 +</​code>​
 +
 +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 [[http://​msdn.microsoft.com/​library/​default.asp?​url=/​library/​en-us/​script56/​html/​dd5dc02a-71e4-412b-8b30-9cc2d3d5e6fb.asp|Microsoft'​s Script Runtime reference.]]
 +
 +\\
 +
 +
 +{{tag>​Developer RhinoScript}}
  
developer/scriptsamples/dictionary.txt ยท Last modified: 2015/09/14 (external edit)