Array Functions

There are 3 VBScript functions that come in handy when working with arrays. These are the UBound, LBound and Split functions.

The UBound() Function

This 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)
- This function will return the highest element available in an array.
- If your array has 10 elements (remember zero base) then the upper bound would be 9.

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() Function

Syntax: LBound(ArrayName)
- This function will return the lowest element available in an array, in most cases 0.

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() Function

The Split function is used to split (break up) a string of characters into an array.

Syntax: Split(String, Delimiter, Count)
- String is the string we will be splitting
- Delimiter is the character that you want the string to be split by.
- Count is the number of elements you want to limit the array to and is optional.

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'
In Part 3 read about 'Multidimensional Arrays'
In part 4 read about 'Dynamic Arrays'

Get the best asp hosting provider from web-hosting-top.com and save up to 30%

Advertisements



MembersPro

MembersPro PayPal - ASP Membership software

Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.

Global ASP.NET Hosting Leader - Click Here