Advantages of Array

Although VBScript arrays make it easy to sort through clusters of information, many programmers ignore them for the simple fact that they’re not as easy to understand as other methods within this scripting language. Arrays can be used to sort through data for all kinds of information. An example would be sorting through a text file line by line. An array can be used to divide each line in that file by any character chosen by the programmer. Any other method within vbscript would require calling out a number of characters from the left or right or looking for specific strings within the line. This is fine, but if the contents of the line are not known, an array may be the best bet. In order to understand arrays, one could go out and buy an “Array for Dummies” book, but it might be easier to run through a quick example of an array which reads a simple text file.

Array Code
Array Code | Source

Colors.txt

colors.txt File
colors.txt File | Source

Create colors.txt File

Go to Start-->All Programs-->Accessories-->Notepad. Go to File-->Save As and save the file as “C:\Temp\colors.txt”. Copy the line containing the colors into the colors.txt file by selecting the line, pressing “Ctrl” + “C”, then selecting in the colors.txt file and pressing “Ctrl” + “V” .

red,green,blue,yellow,orange,brown

Select File-->Save within Notepad. Exit Notepad

Create array.vbs File

Go to Start-->All Programs-->Accessories-->Notepad. Go to File-->Save As and save the file as “C:\Temp\array.vbs”. Adding the .vbs file extension creates an executable VBScript file. Copy the following code into the array.vbs file by selecting the code just to the left of “Option Explicit”, dragging the cursor just to the right of “WScript.Quit(), press “Ctrl” + “C”, select within the array.vbs file, then press“Ctrl” + “V”. Select File-->Save within Notepad. Exit Notepad

Option Explicit

'Declare Variables

Public objFSO

Public wshShell

Public ObjDictionary

Public strKey

Public objReadFile

Dim strLine

Dim fileNameArray

Dim i

'Set Objects

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set wshShell = CreateObject( "WScript.Shell" )

Set objDictionary = CreateObject("Scripting.Dictionary")

'Setup object to read the colors.txt file.

Set objReadFile = objFso.OpenTextFile("c:\temp\colors.txt", 1, False)

'Set the first line to the strLine variable.

strLine = objReadFile.ReadLine

'Trim the line so there are no leading or trailing characters.

strLine = trim(strLine)

'Create the array. Separate each item in the array by a comma.

fileNameArray = split(strLine,",")

'Each item in the array has a number associated with it starting at

'"0". Ubound is used so that a max number is not needed. An array

'will always start with 0, so if there are 5 items in the array,

'item1 will be "0", item2 will be "1", item3 will be "2" and so on.

For i = 0 to Ubound(fileNameArray)

Next

'Display results in messagebox.

Wscript.echo "Item2 in the array is " & fileNameArray(2) & ", and item5 in the array is " & fileNameArray(5) & "."

'clean objects from memory.

Set objFSO = nothing

Set wshShell = nothing

Set objDictionary = Nothing

WScript.Quit()

Messagebox

Messagebox
Messagebox | Source

Numbered Array Items

Array Items Numbered
Array Items Numbered | Source

Run VBScript

Navigate to “C:\Temp\” and double click on the array.vbs file. A message will pop up stating that item2 in the array is blue and item5 in the array is brown. The VBScript is reading the text file, dividing the line by commas and making each word an item in the array. Since arrays start with 0, the sixth item in the array is labeled with a 5. The picture to the right actually shows how the items are numbered by the array. The example code also has comments in it which explain this as well.

Changing the Wscript.echo line in the script is another way to understand how the array works. For example, fileNameArray(2) can be changed to fileNameArray(0), fileNameArray(3)or fileNameArray(4) to display different colors in the messagebox. The "item2" text would need to be changed to the corresponding fileNameArray(#) for the message to make sense.

More by this Author


Comments

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working