In ASP, an array is a set of variables with the same name, with multiple elements in the array, separated by different subscript values from each element of the array. In VBScript, there are two types of arrays: static arrays and dynamic arrays.
1. Static array
Static arrays open up the memory area at compile time, and the array size cannot be changed at run time.
Define a one-dimensional array mmarray (3)
Dim Mmarray (3)
Mmarray (0) =1
Mmarray (1) =3
Mmarray (2) =5
Mmarray (3) =7
Where Mmarray is the array name, the lower bound of the array is 0, the upper bounds are 3, the array elements are from Mmarray (0) to Mmarray (3), and there are 4 elements.
You can also define this:
Dim MyArray
Myarray=array (1,3,5,7)
n=10
Dim MyArray (N) ' This defines an array error
To define a static array, you must determine the upper bounds of the array, and the upper bounds of the array cannot be variables. Because a static array is a memory area that is opened at compile time.
2. Dynamic array
A dynamic array is a variable-size array at run time. When the program is not running, the dynamic array does not account for memory and only opens up memory when the program is running.
The definition of a dynamic array is generally two-step:
First, use the Dim statement to declare an array in parentheses that does not contain subscripts.
Then, use the ReDim statement to redefine the subscript value as needed before using the array.
You can also define an array directly with the ReDim statement. For example
ReDim A (2)
A (0) =1
A (1) =2
A (2) =3
The format of the ReDim statement is: ReDim [Preserve] Variable (subscript)
For example, define a dynamic array arrorg
Dim arrorg ()
ReDim arrorg (3) ' Before using arrorg, the actual number of elements must be allocated with the REDIM statement
You can change the number of elements with ReDim
Dim arrorg ()
ReDim arrorg (3)
ReDim arrorg (5)
ReDim arrorg (2)
Every time the ReDim statement is executed, the current values stored in the array are all lost. If you want to change the size of the array without losing the data in the array, use the keyword preserve.
<%
ReDim A (2)
A (0) =1
A (1) =2
A (2) =3
ReDim Preserve A (1)
A (1) =9
For N=0 to UBound (a)
Response.Write A (n) & "
”
Next
%>
ReDim Preserve arrorg (Ubound (arrorg) +1)
The above code expands an array by one element, and the existing element value is unchanged. The Ubound () function returns the upper bound of the array.
Note: When you redefine an array with the ReDim statement, you can only change the number of elements in the array, not the dimensions of the array.
3. Why use dynamic arrays?
When the program is not running, the dynamic array does not account for memory and only opens up memory when the program is running.
A dynamic array is a variable-size array at run time.
The advantage of using dynamic arrays is that storage space can be used efficiently according to user needs.
If you put some data into an array, using dynamic arrays would be a good idea if you were unsure of how many.
For example, there are 100 million of data. Only 100 of the required data is placed in the array. It would be wise to choose a dynamic array method.
The following example places a number that is divisible by 54 in 1000 into an array.
Dim MyArray () ' declares an array in parentheses that does not contain subscripts.
Dim I,j
J=0
For i=0 to 1000
IF I Mod 54=0 then
ReDim Preserve MyArray (j)
MyArray (j) =i
J=j+1
End IF
Next
' Output
Response.Write "1000 is divisible by 54 in total:" & Ubound (MyArray) +1 & ", respectively:
for I=0 to Ubound (MyArray)
Response.Write MyArray (i) & "
"
Next
This example shows that you are unsure of the number of array elements, but if you directly define the DI M MyArray (1000), is extremely foolish.
One is that the definition will waste storage space. The actual array element required in this example is 19
Two, so that code execution is inefficient, resulting in wasted resources. For example, reordering this element. The Dim MyArray (1000) is defined directly and will traverse 1000 times.
Extensible Array, program code as follows
Dim MyArray ()
for i=0 to ten
ReDim Preserve MyArray (i)
MyArray (i) =i
Next
One character String splits and returns an array of split results, the program code is as follows
Dim tmpstr,myarray
tmpstr= "1,3,5,7,9"
Myarray=split (Tmpstr, ",")
for N=0 to Ubound (MyArray)
Response.Write MyArray (N) & "
"
Next
Using arrays in application and sessions (the session uses the same method as application)
Take application as an example and put the array in the global cache. The program code is as follows
Application.Lock
Application ("Apparray") =myarray
Application.UnLock
When you change a global cache array, you must assign the global cache array to the variable, change the value of an element of the variable, and then place the variable in the global cache. You cannot change the value of a global cache element without modifying it directly. Please analyze the following examples:
<%
Dim Myarray,tmparr
Myarray=array (a)
Application.Lock
Application ("Apparray") =myarray
Application.UnLock
Response.Write Application ("Apparray") (1) & "
”
' The following methods are incorrect
Application.Lock
Application ("Apparray") (1) =9
Application.UnLock
Response.Write Application ("Apparray") (1) & "
”
' The following methods are correct
Tmparr=application ("Apparray")
Tmparr (1) =9
Application.Lock
Application ("Apparray") =tmparr
Application.UnLock
Response.Write Application ("Apparray") (1) & "
”
%>
The result is:
2
2
9
Web development tutorial The use of static arrays and dynamic arrays in--asp