ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Advantages of Array

Updated on August 6, 2016

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.

working

This website uses cookies

As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://corp.maven.io/privacy-policy

Show Details
Necessary
HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
LoginThis is necessary to sign in to the HubPages Service.
Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
AkismetThis is used to detect comment spam. (Privacy Policy)
HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
Features
Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
MavenThis supports the Maven widget and search functionality. (Privacy Policy)
Marketing
Google AdSenseThis is an ad network. (Privacy Policy)
Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
Index ExchangeThis is an ad network. (Privacy Policy)
SovrnThis is an ad network. (Privacy Policy)
Facebook AdsThis is an ad network. (Privacy Policy)
Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
AppNexusThis is an ad network. (Privacy Policy)
OpenxThis is an ad network. (Privacy Policy)
Rubicon ProjectThis is an ad network. (Privacy Policy)
TripleLiftThis is an ad network. (Privacy Policy)
Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
Statistics
Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
ClickscoThis is a data management platform studying reader behavior (Privacy Policy)