April 05, 2020

VBScript tutorial 7 | Arrays

This is the next tutorial in the VBScript Tutorials for Beginners. This VBScript beginner tutorial explains the VBScript array variable. Please view the VBScript tutorial 7 or read on... What is array in VBScript? An array variable in VBScript can store multiple values in it. Such data values may be customer names or phone numbers or email addresses etc. A VBScript array has indexes to refer the different values in it. The indexes start with the index 0. In the VBScript example below, the Sub FixedArray shows an array example, strCustomers(3). We can run this VBScript in the Command Prompt using the CScript  command e.g. CScript Array1.vbs

' VBScript code
Option Explicit
Call FixedArray
Call DynamicArray

Sub FixedArray
    ' Declare a fixed array i.e. an array with the specified number of elements.
    Dim strCustomers(3)
    strCustomers(0) = "Abe"
    strCustomers(1) = "Ben"
    strCustomers(2) = "Chris"
    strCustomers(3) = "Dustin"
    ' Display the first data value in the command window, instead of a message box.
    WScript.Echo "strCustomers(0) is " &  strCustomers(0)
End Sub

Sub DynamicArray
    ' Declare a dynamic array i.e. an array whose number of elements is unknown at present.
    Dim strCustomersNew()
    ' VBScript Redim statement defines the number of elements in the array.
    Redim strCustomersNew(3)
    strCustomersNew(0) = "Abe"
    strCustomersNew(1) = "Ben"
    strCustomersNew(2) = "Chris"
    strCustomersNew(3) = "Dustin"
    ' Preserve in the Redim statement retains the existing array elements.
    Redim Preserve strCustomersNew(5)
    strCustomersNew(4) = "Eddie"
    strCustomersNew(5) = "Fred"
    Dim i
    ' VBScript UBound function gives the upper bound of the array.
    For i = 0 to UBound(strCustomersNew)
        WScript.Echo "The element" &  i & " is " &  strCustomersNew(i)
    Next
End Sub

Next, let us see the VBScript code with an array of numbers.

' VBScript code 
Option Explicit
On Error Resume Next
' Declare a array with indexes 0 through 5 i.e. with 6 elements.
Dim Numbers(5)
FillNumbers
AverageNumbers
' Sort array, Numbers in ascending order.
Sort

Sub FillNumbers
    Numbers(0) = 10
    Numbers(1) = 2
    Numbers(2) = 3
    Numbers(3) = 8
    Numbers(4) = 12
    Numbers(5) = 1   
End Sub

Sub AverageNumbers
    Dim intTotal, i
    intTotal = 0
    For i = 0 to 5
        intTotal = intTotal + Numbers(i)
    Next
    ' Round VBScript function rounds off intTotal to 2 decimal places below.
    WScript.Echo "The average is " & Round(intTotal/6, 2)
End Sub

Sub Sort
    ' Use bubble sort algorithm to sort Numbers array in ascending order.
    Dim i, j, temp, strNumbers
    For i = 0 to 4
        ' Define a nested loop for swapping elements to their correct positions.
        For j = 0 to 4 - i
           ' Change > to < to sort in descending order.
           If Numbers(j) > Numbers(j+1) Then 'Swap the two array elements
                temp = Numbers(j)
                Numbers(j) = Numbers(j+1)
                Numbers(j+1) = temp
            End If
        Next
        strNumbers = ""
        For j = 0 to 5
            strNumbers = strNumbers & Numbers(j) & " "
        Next
        WScript.Echo "i = " & i & " The array is " & strNumbers       
    Next
End Sub

Want to learn how the sub procedures above work, in detail? Please view my VBScript tutorial 7. Thank you.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.