Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for arraylist.vbs

(view source code of arraylist.vbs as plain text)

  1. ' ArrayLst.vbs,  Version 1.00
  2. ' Demonstrates the use of the .NET Frameworks ArrayList class in VBScript
  3. '
  4. ' Based on the "Hey, Scripting Guy!" article "Be Careful What You Say"
  5. ' http://www.microsoft.com/technet/technetmag/issues/2007/01/HeyScriptingGuy/default.aspx
  6. '
  7. ' Written by Rob van der Woude
  8. ' http://www.robvanderwoude.com
  9.  
  10. Option Explicit
  11.  
  12. Dim DataList, SecondList
  13.  
  14. ' create the first ArrayList
  15. Set DataList = CreateObject( "System.Collections.ArrayList" )
  16.  
  17. ' adding elements is really a snap
  18. DataList.Add "F"
  19. DataList.Add "B"
  20. DataList.Add "D"
  21. DataList.Add "C"
  22.  
  23. ' show the content and size of the ArrayList
  24. ' Note: ShowList is a custom subroutine,
  25. '       not a "native" VBScript statement
  26. ShowList DataList, "Initial 4 elements"
  27.  
  28. ' add some more elements
  29. DataList.Add "E"
  30. DataList.Add "D"
  31. DataList.Add "A"
  32. DataList.Add "D"
  33.  
  34. ' (show the result)
  35. ShowList DataList, "Append another 4"
  36.  
  37. ' removing an element by its name isn't difficult either
  38. DataList.Remove "D"
  39.  
  40. ' (show the result)
  41. ShowList DataList, "Remove D"
  42.  
  43. ' sorting doesn't require multiple lines of code
  44. DataList.Sort
  45.  
  46. ' (show the result)
  47. ShowList DataList, "Sort"
  48.  
  49. ' neither does reversing the order of the elements
  50. DataList.Reverse
  51.  
  52. ' (show the result)
  53. ShowList DataList, "Reverse"
  54.  
  55. ' remove another element by its name
  56. DataList.Remove "D"
  57.  
  58. ' (show the result)
  59. ShowList DataList, "Removed D"
  60.  
  61. ' remove another element
  62. DataList.Remove "D"
  63.  
  64. ' (show the result)
  65. ShowList DataList, "Remove D"
  66.  
  67. ' create a second ArrayList
  68. Set SecondList = CreateObject( "System.Collections.ArrayList" )
  69.  
  70. ' populate it with some data
  71. SecondList.Add "3"
  72. SecondList.Add "8"
  73. SecondList.Add "2"
  74. SecondList.Add "7"
  75. SecondList.Add "5"
  76. SecondList.Add "1"
  77. SecondList.Add "5"
  78.  
  79. ' (show the result)
  80. ShowList SecondList, "Initial"
  81.  
  82. ' add the data from the second ArrayList to the first one
  83. DataList.AddRange( SecondList )
  84.  
  85. ' (show the result)
  86. ShowList DataList, "AddRange"
  87.  
  88. ' sort the (first) ArrayList
  89. DataList.Sort
  90.  
  91. ' (show the result)
  92. ShowList DataList, "Sort"
  93.  
  94. ' remove some elements
  95. DataList.Remove "3"
  96. DataList.Remove "B"
  97.  
  98. ' (show the result)
  99. ShowList DataList, "Remove 3, B"
  100.  
  101. ' reduce the capacity of the ArrayList to the minimum required
  102. DataList.TrimToSize
  103.  
  104. ' (show the result)
  105. ShowList DataList, "TrimToSize"
  106.  
  107. ' remove an element by its index
  108. DataList.RemoveAt 0
  109.  
  110. ' (show the result)
  111. ShowList DataList, "RemoveAt 0"
  112.  
  113. ' remove another element by its index
  114. DataList.RemoveAt 3
  115.  
  116. ' (show the result)
  117. ShowList DataList, "RemoveAt 3"
  118.  
  119. ' display the first index of a named element
  120. WScript.Echo "IndexOf( ""A"", 0 )      " & DataList.IndexOf( "A", 0 )
  121.  
  122. ' remove a range of indexes from the ArrayList
  123. DataList.RemoveRange 3, 2
  124.  
  125. ' (show the result)
  126. ShowList DataList, "RemoveRange 3, 2"
  127.  
  128. ' display the first index of an element with value "5"
  129. WScript.Echo "IndexOf( ""5"", 0 )      " & DataList.IndexOf( "5", 0 )
  130. ' display the first index after index 1 of an element with value "2"
  131. WScript.Echo "IndexOf( ""2"", 1 )      " & DataList.IndexOf( "2", 1 )
  132. ' display the last index of an element with value "5"
  133. WScript.Echo "LastIndexOf( ""5"" )     " & DataList.LastIndexOf( "5" )
  134. WScript.Echo
  135.  
  136. ' delete all elements from the second ArrayList
  137. SecondList.Clear
  138. ' add new elements
  139. SecondList.Add "I"
  140. SecondList.Add "II"
  141. SecondList.Add "III"
  142. SecondList.Add "IV"
  143. SecondList.Add "V"
  144. ' reduce the capacity to the minimum required
  145. SecondList.TrimToSize
  146.  
  147. ' (show the result)
  148. ShowList SecondList, "Initial"
  149.  
  150. ' insert an element with value "X" at index 2
  151. SecondList.Insert 2, "X"
  152.  
  153. ' (show the result)
  154. ShowList SecondList, "Insert 2, ""X"""
  155.  
  156. ' add the data from the second ArrayList to the first one
  157. DataList.AddRange( SecondList )
  158.  
  159. ' (show the result)
  160. ShowList DataList, "AddRange"
  161.  
  162. ' demonstrate some other functionality available for ArrayLists
  163. WScript.Echo "DataList.Contains( ""E"" )   = " & DataList.Contains( "E" )
  164. WScript.Echo "DataList.IndexOf( ""E"", 0 ) = " & DataList.IndexOf( "E", 0 )
  165. WScript.Echo "DataList.Contains( ""M"" )   = " & DataList.Contains( "M" )
  166. WScript.Echo "DataList.IndexOf( ""M"", 0 ) = " & DataList.IndexOf( "M", 0 )
  167. WScript.Echo "DataList( 4 )              = " & DataList( 4 )
  168. WScript.Echo "DataList.Item( 4 )         = " & DataList.Item( 4 )
  169.  
  170.  
  171.  
  172.  
  173. Sub ShowList( ByRef arrListName, ByVal strPrefix )
  174. ' Custom subroutine displaying the contents of an ArrayList on a
  175. ' single line, and the capacity and size on a separate second line.
  176. ' Arguments used are:
  177. ' arrListName:  the name of the ArrayList to be displayed
  178. ' strPrefix:    a short description to be displayed before the
  179. '               actual content of the ArrayList
  180.  
  181. 	Dim intLen, strItem, strList
  182.  
  183. 	' specify the maximum length of the descriptive text
  184. 	intLen = 20
  185.  
  186. 	' save the ArrayList content to a string
  187. 	For Each strItem in arrListName
  188. 	    strList = strList & " " & strItem
  189. 	Next
  190.  
  191. 	' trim or pad the description to its maximum length, append the ArrayList content string, and display it
  192. 	WScript.Echo Left( strPrefix  & Space( intLen ), intLen ) & ": "  & strList
  193.  
  194. 	' display the ArrayList's current size and capacity
  195. 	WScript.Echo Left( "Count/Capacity" & Space( intLen ), intLen ) & ":  " & arrListName.Count & "/" & arrListName.Capacity
  196. 	WScript.Echo
  197. End Sub
  198.  

page last uploaded: 2016-12-15, 11:20