(view source code of arraylist.vbs as plain text)
' ArrayLst.vbs, Version 1.00
' Demonstrates the use of the .NET Frameworks ArrayList class in VBScript
'
' Based on the "Hey, Scripting Guy!" article "Be Careful What You Say"
' http://www.microsoft.com/technet/technetmag/issues/2007/01/HeyScriptingGuy/default.aspx
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com
Option Explicit
Dim DataList, SecondList
' create the first ArrayList
Set DataList = CreateObject( "System.Collections.ArrayList" )
' adding elements is really a snap
DataList.Add "F"
DataList.Add "B"
DataList.Add "D"
DataList.Add "C"
' show the content and size of the ArrayList
' Note: ShowList is a custom subroutine,
' not a "native" VBScript statement
ShowList DataList, "Initial 4 elements"
' add some more elements
DataList.Add "E"
DataList.Add "D"
DataList.Add "A"
DataList.Add "D"
' (show the result)
ShowList DataList, "Append another 4"
' removing an element by its name isn't difficult either
DataList.Remove "D"
' (show the result)
ShowList DataList, "Remove D"
' sorting doesn't require multiple lines of code
DataList.Sort
' (show the result)
ShowList DataList, "Sort"
' neither does reversing the order of the elements
DataList.Reverse
' (show the result)
ShowList DataList, "Reverse"
' remove another element by its name
DataList.Remove "D"
' (show the result)
ShowList DataList, "Removed D"
' remove another element
DataList.Remove "D"
' (show the result)
ShowList DataList, "Remove D"
' create a second ArrayList
Set SecondList = CreateObject( "System.Collections.ArrayList" )
' populate it with some data
SecondList.Add "3"
SecondList.Add "8"
SecondList.Add "2"
SecondList.Add "7"
SecondList.Add "5"
SecondList.Add "1"
SecondList.Add "5"
' (show the result)
ShowList SecondList, "Initial"
' add the data from the second ArrayList to the first one
DataList.AddRange( SecondList )
' (show the result)
ShowList DataList, "AddRange"
' sort the (first) ArrayList
DataList.Sort
' (show the result)
ShowList DataList, "Sort"
' remove some elements
DataList.Remove "3"
DataList.Remove "B"
' (show the result)
ShowList DataList, "Remove 3, B"
' reduce the capacity of the ArrayList to the minimum required
DataList.TrimToSize
' (show the result)
ShowList DataList, "TrimToSize"
' remove an element by its index
DataList.RemoveAt 0
' (show the result)
ShowList DataList, "RemoveAt 0"
' remove another element by its index
DataList.RemoveAt 3
' (show the result)
ShowList DataList, "RemoveAt 3"
' display the first index of a named element
WScript.Echo "IndexOf( ""A"", 0 ) " & DataList.IndexOf( "A", 0 )
' remove a range of indexes from the ArrayList
DataList.RemoveRange 3, 2
' (show the result)
ShowList DataList, "RemoveRange 3, 2"
' display the first index of an element with value "5"
WScript.Echo "IndexOf( ""5"", 0 ) " & DataList.IndexOf( "5", 0 )
' display the first index after index 1 of an element with value "2"
WScript.Echo "IndexOf( ""2"", 1 ) " & DataList.IndexOf( "2", 1 )
' display the last index of an element with value "5"
WScript.Echo "LastIndexOf( ""5"" ) " & DataList.LastIndexOf( "5" )
WScript.Echo
' delete all elements from the second ArrayList
SecondList.Clear
' add new elements
SecondList.Add "I"
SecondList.Add "II"
SecondList.Add "III"
SecondList.Add "IV"
SecondList.Add "V"
' reduce the capacity to the minimum required
SecondList.TrimToSize
' (show the result)
ShowList SecondList, "Initial"
' insert an element with value "X" at index 2
SecondList.Insert 2, "X"
' (show the result)
ShowList SecondList, "Insert 2, ""X"""
' add the data from the second ArrayList to the first one
DataList.AddRange( SecondList )
' (show the result)
ShowList DataList, "AddRange"
' demonstrate some other functionality available for ArrayLists
WScript.Echo "DataList.Contains( ""E"" ) = " & DataList.Contains( "E" )
WScript.Echo "DataList.IndexOf( ""E"", 0 ) = " & DataList.IndexOf( "E", 0 )
WScript.Echo "DataList.Contains( ""M"" ) = " & DataList.Contains( "M" )
WScript.Echo "DataList.IndexOf( ""M"", 0 ) = " & DataList.IndexOf( "M", 0 )
WScript.Echo "DataList( 4 ) = " & DataList( 4 )
WScript.Echo "DataList.Item( 4 ) = " & DataList.Item( 4 )
Sub ShowList( ByRef arrListName, ByVal strPrefix )
' Custom subroutine displaying the contents of an ArrayList on a
' single line, and the capacity and size on a separate second line.
' Arguments used are:
' arrListName: the name of the ArrayList to be displayed
' strPrefix: a short description to be displayed before the
' actual content of the ArrayList
Dim intLen, strItem, strList
' specify the maximum length of the descriptive text
intLen = 20
' save the ArrayList content to a string
For Each strItem in arrListName
strList = strList & " " & strItem
Next
' trim or pad the description to its maximum length, append the ArrayList content string, and display it
WScript.Echo Left( strPrefix & Space( intLen ), intLen ) & ": " & strList
' display the ArrayList's current size and capacity
WScript.Echo Left( "Count/Capacity" & Space( intLen ), intLen ) & ": " & arrListName.Count & "/" & arrListName.Capacity
WScript.Echo
End Sub
page last modified: 2023-03-10