Search this site...

Arrays in ASP

The variables make programming more efficient because then a variable can hold any numerical value or result of numerical expression. The variables are great for holding one data value at a time. But what if you wanted to store many similar data type values with a common name? Arrays are the answer and we'll discuss them next.

What is an array?

An array is a contiguous space of memory allocated for storing some data. We know when variables are declared, computer's memory is allocated for them. A similar process takes place when arrays are declared except that size of the memory allocated is greater, depending on the size of the array. Each memory allocation in the array is referred to as a cell or index. If the size of our array is 10, we have 10 different cells. Figure 1 compares a variable declaration with an array declaration. An array index starts from 0 and ends at the size of the array, minus 1. If array's size is 10, its first index will be 0 and the last index will be 9. We next discuss how to declare and reference arrays with the indices.

Figure 1 array and variable representation
Figure 1 array and variable representation

How to declare and reference an array?

An array is declared with the keyword DIM and then the array name and then the size of the array in parentheses. Like variables, we do not specify the data type of an array. The following shows the general syntax for declaring an array:

DIM ArrayName(ArraySize)

In the above statement ArrayName is the name of the array and ArraySize is the size of the array. In the following,

DIM strWeekdays(6)

we declare an array called strWeekdays and of size 7. We use the prefix str to clarify that the array is of strings. The array will hold names of days in a week. An array can be initialized in one of two ways. The first method is long because each cell is initialized individually. The second method uses the Array () function to initialize the array.

To understand how to assign or initialize values to an array, you should understand first how to access the values of an array. To access a cell of an array, we use the array name and an index value. A value assigned to a cell of an array is referred to as element. If we want to access the first element of an array, we will use the following syntax: arrayName(0). If we wanted to access the third element, we would use the following syntax: arrayName(2).

Now, we can use the information of how to access an array to initialize an array. If we wanted to initialize a value to the first and third element of an array, we will use the following syntax:

arrayName(0) = value or an expression
arrayName(2) = value or an expression

The first statement above assigns a value to the first element of the array and the second statement assigns a value to the third element of an array. In the following example,

DIM strWeekdays(6)
DIM sout, i
strWeekdays(0) = "Sunday"
strWeekdays(1) = "Monday"
strWeekdays(2) = "Tuesday"
strWeekdays(3) = "Wednesday"
strWeekdays(4) = "Thursday"
strWeekdays(5) = "Firday"
strWeekdays(6) = "Saturday"
for i = 0 to 6
sout = sout & "strWeekdays(" & i & ") = "
sout = sout & strWeekdays(i) & "<br>"
next
response.write sout

we use an array called strWeekdays and of size 7. On lines 3 through 9, we initialize this array to the days of a week. Then, we use a for loop to print the content of the array. See the following for the output of the above code:

strWeekdays(0) = Sunday
strWeekdays(1) = Monday
strWeekdays(2) = Tuesday
strWeekdays(3) = Wednesday
strWeekdays(4) = Thursday
strWeekdays(5) = Firday
strWeekdays(6) = Saturday

Array () function

An Array () function can be used to initialize values to an array. The following initializes an array without referencing each of the cells in an array of size 3:

arrayName = Array (value1, value2, vlaue3)

The function Array () initializes the each of the values or parameters to the corresponding index. For example, "value1" will be assigned to index 0 and value3 will be assigned to index 2. The following statement initializes our strWeekdays2 array:

strWeekdays2 = Array ("Sunday", "Monday", "Tuesday", "Wednesday",_
"Thursday", "Friday", "Saturday")

Note above, we are using "_" (underscore) line continuation character. The line continuation character can be used for long statements. The line continuation character simply means a line is continued on the next line.

Clearly, using array function is much simpler than accessing each of the indices of the array and then assigning a value to the array. Like our previous array example for strWeekdays, Sunday is assigned to index 0, Monday is assigned to index 1,...,Saturday is assigned to index 6. Also note the size of the array strWeekdays2 is 7 and the first index is 0 and the last is 6. When you use the array () function, the size of the array is determined by the number of values passed as argument. In the following, we use the array () function:

DIM strWeekdays2, sout, i
strWeekdays2 = Array ("Sunday", "Monday", "Tuesday", "Wednesday",_
"Thursday", "Friday", "Saturday")
for i = 0 to 6
sout = sout & "strWeekdays2(" & i & ") = "
sout = sout & strWeekdays2(i) & "<br>"
next
response.write sout

The strWeekdays2 array is initialized above on line 2 and 3. We then use a for loop to print the content of the array. The output is the same as you saw for the strWeekdays array:

strWeekdays2(0) = Sunday
strWeekdays2(1) = Monday
strWeekdays2(2) = Tuesday
strWeekdays2(3) = Wednesday
strWeekdays2(4) = Thursday
strWeekdays2(5) = Friday
strWeekdays2(6) = Saturday