WEB development tutorial -- usage of static and dynamic arrays in ASP, -- asp Array

Source: Internet
Author: User

WEB development tutorial -- usage of static and dynamic arrays in ASP, -- asp Array

In ASP, an array is a group of variables with the same name. An array contains multiple elements, and each element of an array is distinguished by different downmarked values. In VBScript, Arrays can be static arrays or dynamic arrays.
1. Static Array
Static arrays are opened up in the memory area during compilation, and the array size cannot be changed during runtime.
Define a one-dimensional array mmArray (3)
Dim mmArray (3)
MmArray (0) = 1
MmArray (1) = 3
MmArray (2) = 5
MmArray (3) = 7
Here, mmArray is the array name. The lower bound of the array is 0 and the upper bound is 3. There are four elements in the array element, from mmArray (0) to mmArray (3.
You can also define it as follows:
Dim MyArray
MyArray = Array (1, 3, 5, 7)
N = 10
An error occurred while defining the array in Dim MyArray (n) '.
Defines a static array. The upper bound of the array must be determined. The upper bound of the array cannot be a variable. Because static arrays open up the memory zone during compilation.
2. Dynamic Array
A dynamic array is an array that is variable in size during running. When the program is not running, the dynamic array does not occupy the memory, and the memory is opened only when the program is running.
The definition of a dynamic array is generally divided into two steps:
First, use the Dim statement to declare an array that does not contain the lower mark in a bracket.
Then, use the ReDim statement to re-define the tag value as needed before using the array.
You can also use the Redim statement to directly define an array. For example
ReDim a (2)
A (0) = 1
A (1) = 2
A (2) = 3
ReDim statement format: ReDim [Preserve] variable (subscript)
For example, defining a dynamic array arrOrg
Dim arrOrg ()
ReDim arrOrg (3) 'before using arrOrg, you must use the ReDim statement to allocate the actual number of elements.
You can use ReDim to change the number of elements.
Dim ArrOrg ()
ReDim arrOrg (3)
ReDim arrOrg (5)
ReDim arrOrg (2)
Each time a ReDim statement is executed, all the current values stored in the array are lost. If you want to change the array size 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 ()
Response. Write a (n) &"
"
Next
%>
ReDim Preserve arrOrg (Ubound (arrOrg) + 1)
The code above expands an array element and the existing element value remains unchanged. The Ubound () function returns the upper bound of the array.
Note: When you use the ReDim statement to redefine an array, you can only change the number of array elements, but not the dimension of the array.
3. Why dynamic arrays?
When the program is not running, the dynamic array does not occupy the memory, and the memory is opened only when the program is running.
A dynamic array is an array that is variable in size during running.
The advantage of using dynamic arrays is that you can effectively use buckets based on your needs.
If you store a data in an array, it is very good to use dynamic arrays without knowing how many data entries are there.
For example, there are 0.1 billion data records. Only 100 data entries that meet the requirements must be placed in the array. It is wise to use dynamic arrays.
In the following example, the number of 54 divisible by 1000 is put into an array.
Dim MyArray () 'declares that an array containing no lower mark is included in a bracket.
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 "Total number of 54 divisible items in 1000:" & Ubound (MyArray) + 1 & ", respectively:

"
For I = 0 to Ubound (MyArray)
Response. Write MyArray (I) &"
"
Next
This example shows that you are not sure about the number of array elements, but it is very stupid to define Dim MyArray (1000) directly.
First, this definition will waste storage space. In this example, 19 array elements are actually required.
Second, such code execution efficiency is low, resulting in a waste of resources. For example, this element is reordered. Define Dim MyArray (1000) directly and traverse it for 1000 times.
Extensible array. The program code is as follows:
Dim MyArray ()
For I = 0 To 10
ReDim Preserve MyArray (I)
MyArray (I) = I
Next
Returns an array of split results after a string is split. 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

Use arrays in Application and Session (the Session usage method is the same as that of Application)
Take Application as an example to put the array in the global cache. The program code is as follows:
Application. Lock
Application ("appArray") = MyArray
Application. Unlock
When changing the global cache array, you must first 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. Instead, you cannot directly modify the value of a global cache element. Analyze the following example:
<%
Dim MyArray, tmpArr
MyArray = array (1, 2, 3)
Application. Lock
Application ("appArray") = MyArray
Application. Unlock
Response. Write Application ("appArray") (1) &"
"
'The following method is incorrect
Application. Lock
Application ("appArray") (1) = 9
Application. Unlock
Response. Write Application ("appArray") (1) &"
"
'The following method is 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.