![]() |
|
|
Array FunctionsThere are 3 VBScript functions that come in handy when working with arrays. These are the UBound, LBound and Split functions. The UBound() FunctionThis function will return the 'index' of the highest element in an array. The index is basically the position of the element in the array. Arrays in ASP/VBScript have a zero based starting index. Syntax: UBound(ArrayName) Example: <%
Dim myArray myArray(0)="spurs" myArray(1)="celtic" myArray(2)="ipswich" myArray(3)="brazil" highest_element=UBound(myArray) response.write highest_element %> Output: 3 The LBound() FunctionSyntax: LBound(ArrayName) Example: <%
Dim myArray(3) myArray(0)="spurs" myArray(1)="celtic" myArray(2)="ipswich" myArray(3)="brazil" lowest_element=LBound(myArray) response.write lowest_element %> Output: 0 The Split() FunctionThe Split function is used to split (break up) a string of characters into an array. Syntax: Split(String, Delimiter, Count) Example: <%
Dim MyString, MyArray MyString = "cigarettes,alcohol,sex,football" MyArray = Split(MyString,",") 'the delimiter is the comma %> The Result would be an array called MyArray with 4 elements. MyArray(0) = "cigarettes"
MyArray(1) = "alcohol" MyArray(2) = "sex" MyArray(3) = "football" Again we could loop through the array and print out the values in each array element. In this example we'll also incorporate the UBound function. <%
Dim MyString, MyArray MyString = "cigarettes,alcohol,sex,football" MyArray = Split(MyString,",") For i=0 to UBound(MyArray) 'the UBound function returns 3 response.write myArray(i) & "<br>" Next 'move on to the next value of i %> Notice that in the code above we used the UBound Function which returns the index of the highest element in the array. In this case it's 3 so the For i loop will loop from 0 to 3.
In Part 1 read about 'Arrays'
Site developed by Michael Wall - Web Design Belfast N.Ireland. |
|