Windows Scripting
  Home arrow Windows Scripting arrow Page 4 - 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 - Methods for working with arrays


    (Page 4 of 5 )

    VBScript provides three different methods specifically for working with arrays.  The first is the Split method, which allows you to create an array from a delimited text string.

    arrNames = Split("John Doe,Jane Smith,Dick Tracy", ",")

    The first parameter is a text string from which to create an array.  The second parameter is a text string containing the delimiter that separates individual elements, in this case a comma.  In this example, arrNames would contain “John Doe”, “Jane Smith”, and “Dick Tracy”

    John,Doe

    Jane,Smith

    Dick,Tracy

    This concept can easily be extended to process comma- or tab-delimited text files.  Take for example the comma-delimited file above.  Each line of the file contains a unique name record that contains a first and last name separated by a comma.

    Const ForReading = 1

    Set objFso = CreateObject("Scripting.FileSystemObject")

    Set objFile = objFso.OpenTextFile("C:names.csv", ForReading)

     

    Do Until objFile.AtEndOfStream

       arrRecord = Split(objFile.ReadLine, ",")

       WScript.Echo "First Name:", arrRecord(0)

       WScript.Echo "Last Name:", arrRecord(1)

       WScript.Echo ""

    Loop

    You can use the FileSystemObject to open the file and read it into the script line by line.  The Split function is used to break out each piece of information.

    VBScript also provides the Join function.  The opposite of the Split function, the Join function is used to create a delimited text string from an array.

    Dim arrNames(2)

     

    arrNames(0) = "John Doe"

    arrNames(1) = "Jane Smith"

    arrNames(2) = "Dick Tracy"

     

    strNames = Join(arrNames, ",")

    The first parameter is an array containing text elements to join together.  The second parameter is a text string indicating the text delimiter used to separate elements.

    Finally, VBScript provides an Array method for creating arrays.  The nice thing about the array method is that it returns a dynamic array, as we’ll see a bit later.

    arrNames = Array("John Doe", "Jane Smith", "Dick Tracy")

    Each parameter passed to the array function becomes an element in the newly- created array.

    More Windows Scripting Articles
    More By Nilpo


     

    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 3 Hosted by Hostway
    Stay green...Green IT