Loop through an array

This ASP scripting code allows you to display the content of an array. In addition, the script shows how to get the lowest and upper boundaries of an array. The script uses an array called arrWeekDays and three functions: GetLowestBoundary (), GetUpperBoundary (), and LoopThroughArray ().

The array arrWeekDays contains the seven days of the week. As you may be familiar with ASP arrays, the first index of the array is 0 and the last index is the total number of elements in the array minus 1. For example, an array consisting of 7 elements stores the first element at index 0 and the last element at index 6. We will verify this below.

The GetLowestBoundary () returns the lowest boundary of the array, which is 0. This indicates the first element of the array is stored at index 0.

The GetUpperBoundary () returns the upper boundary of the array, which is 6. This indicates the last element of the array is at index 6.

Finally, our LoopThroughArray () function, as the name implies, loops through the array to print each value in the array.

Scripting code

<%
dim arrWeekDays, shtml ' declares two variables
' create an array consisting of seven days of week
arrWeekDays = Array("Sunday","Monday","Tuesday","Wednesday",_
"Thursday","Friday","Saturday")

' get and store the lowest boundary info of the array
shtml = "Lowest boundary of our array: " & GetLowestBoundary (arrWeekDays)

' get and store the upper boundary info of the array
shtml=shtml & "<br>Upper boundary of our array: " & GetUpperBoundary(arrWeekDays)

' get and store the content of the array
shtml=shtml & "<br>Content of our array: " & LoopThroughArray (arrWeekDays)

' now print the value of shtml
response.write shtml

' returns the lowest boundary of an array.
' input: the name of the array
function GetLowestBoundary (arrName)
GetLowestBoundary = LBound(arrName)
end function

' returns the upper boundary of an array.
' input: the name of the array
function GetUpperBoundary (arrName)
GetUpperBoundary = UBound(arrName)
end function

' returns the content of an array.
' Note the output is returned in an unordered list format
' input: the name of the array
function LoopThroughArray (arrName)
dim sout, arrValue
sout = "<ul>" ' start unordered list
for each arrValue in arrName ' loop through the array
sout = sout & "<li>" & arrValue & "</li>" ' store each value in array
next
sout = sout & "</ul>" ' end list
LoopThroughArray = sout ' return the output
end function
%>

Output of the above code:

Lowest boundary of our array: 0
Upper boundary of our array: 6
Content of our array:
  • Sunday
  • Monday
  • Tuesday
  • Wednesday
  • Thursday
  • Friday
  • Saturday