| Answer: |
There are a number of ways you can display the contents of a single-dimension array. The first, and simplest, is to simple use a For ... Next loop to iterate through each element in the array. You can find the lower and upper bounds of an array by using the LBound and UBound functions, respectively. (For more information on LBound and UBound, be sure to read the FAQ: How can I determine the upper or lower bounds of an array?)
The following snippet of code displays each element of the array aFoo using a For ... Next loop.
'Create the aFoo array Dim aFoo aFoo = Array("Hello, ", "World!", "How ", "are ", "you?")
Dim iLoop For iLoop = LBound(aFoo) to UBound(aFoo) Response.Write aFoo(iLoop) & "<BR>" Next
|
The above code will produce the following output (when viewed through a browser):
Hello, World! How are you?
Fairly simple and straightforward. There is another way to display the contents of a single-dimension array that looks much cooler, in my opinion! VBScript provides a join function, which turns the contents of an array into a string using a specified delimiter between each element of the array in the string. To display each element of an array, followed by a <BR>, use the following code:
'Create the aFoo array Dim aFoo aFoo = Array("Hello, ", "World!", "How ", "are ", "you?")
Response.Write join(aFoo, "<BR>")
|
This will produce the exact same output as the code snippet we first examined, but it just looks cooler, requires less code, etc. For more information on join, be sure to check out this FAQ: How can I convert the contents of an array into a string?
Happy Programming! |