Windows Scripting
  Home arrow Windows Scripting arrow Page 5 - Working With Arrays in VBScript
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Working With Arrays in VBScript
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-01-05

    Table of Contents:
  • Working With Arrays in VBScript
  • Creating Arrays
  • Using Values from an Array
  • Methods for working with arrays
  • Dynamic and Multidimensional Arrays

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Working With Arrays in VBScript - Dynamic and Multidimensional Arrays


    (Page 5 of 5 )

    VBScript also allows you to create more advanced types of arrays: arrays whose sizes can be changed dynamically and arrays with multiple sets of elements.  These types of arrays allow you to process groups of data or dynamic data more easily.

    A dynamic array is really not a new type of array.  It’s simply an array whose size can be changed dynamically.  This is accomplished through the use of the ReDim statement.

    arrNames = Array()

    ReDim arrNames(2)

     

    arrNames(0) = "John Doe"

    arrNames(1) = "Jane Smith"

    arrNames(2) = "Dick Tracy"

    In this example, I’m using the Array function to create an empty dynamic array.  Then I use the ReDim statement to resize the array before assigning values.

    ReDim Preserve arrNames(3)

    arrNames(3) = "Nilpo"

    The ReDim statement will empty all elements in an array while resizing it.  You can prevent this by using the Preserve keyword.  If the size is being increased, it simply adds empty elements.  When decreasing, it will retain any elements that fit inside the new dimension and discard the rest.

    This can be extended to create a very powerful mechanism for creating arrays dynamically.  For example, I could read a text file and assign each line of that file to an array element.  Since I may or may not know how many lines are in the text file, it’s quite handy to do this dynamically.

    Const ForReading = 1

    Set objFso = CreateObject("Scripting.FileSystemObject")

    Set objFile = objFso.OpenTextFile("C:myfile.txt", ForReading)

     

    arrLines = Array()

    intCounter = 0

     

    Do Until objFile.AtEndOfStream

       ReDim Preserve arrLines(intCounter)

       arrLines(intCounter) = objFile.ReadLine

       intCounter = intCounter + 1

    Loop

     

    objFile.Close

    You can see here where I’m reading each line of a text file into an array.  I dynamically resize the array element (to increase its size by 1) before adding each new line.  The Preserve keyword ensures that existing data remains intact through each resize, and the intCounter variable is incremented to track the number of elements in the array.

    ReDim Preserve arrNames( UBound(arrNames) + 5 )

    There may also be times when you wish to resize an array without knowing how many elements it contains.  The example above adds five more elements to the arrNames array.  I’ve used the UBound function to return the upper boundary of the array and add 5 to that number.  The upper boundary represents the last element in the array.

    Another advanced type of array is the multi-dimensional array.  This is like an array of arrays—each element contains a group of sub-elements in much the same way as each row of a worksheet contains several other columns.  For example, I could create an array of names.  Each element of that array would represent a person’s name, but would contain sub-elements that contain their first and last names separately.

    Dim arrNames(2, 1)

     

    arrNames(0,0) = "John"

    arrNames(0,1) = "Doe"

     

    arrNames(1,0) = "Jane"

    arrNames(1,1) = "Smith"

     

    arrNames(2,0) = "Dick"

    arrNames(2,1) = "Tracy"

    The syntax for a multi-dimensional array is the same as any other array.  You just add each additional dimension, separated by a comma.  The arrNames array contains three parent elements.  Each parent element contains two sub-elements as defined by the Dim statement.  Each element can then be accessed by providing the proper dimensions.  That’s why arrNames(2,1) represents the second sub-element of the third element in the arrNames array.

    Dim arrNames(0)

    arrNames(0) = Array("John", "Doe")

     

    WScript.Echo arrNames(0,1)   'Does not work

    WScript.Echo arrNames(0)(1)  'Does work

    In this example, I’ve attempted to create a multi-dimensional array by assigning an array to another array element.  This code works perfectly; however, I cannot access the sub-elements as I should be able to.  Why?  A multi-dimensional array is like an array of arrays—it’s not actually the same thing.  This code adds an array reference to the first array, but does not actually add the second array’s data.  This is still stored in memory in the second array.  All I’ve really done here is nest two arrays.

    As you can see, there are several ways to utilize arrays in VBScript.  They can be very powerful mechanisms for accessing and processing data, but you have to be sure that they are created and handled properly.  Go ahead and play around with these code samples.  You’ll gain a better understanding through trial and error.  Until next time, keep coding!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    WINDOWS SCRIPTING ARTICLES

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT