ASP Dynamic Array

Source: Internet
Author: User
A defines an array

There are two methods: dim and redim.

Dim defines arrays of fixed numbers and data types, while redim defines different types of data, or the number is not fixed. Compare the following examples. Examples:

Program Code

Dim myarray (5, 2)
Redim myarray (5, 2)


The former is incorrect while the latter is valid. Example:

N = 10 N = 10

Dim myarray (N)
Redim myarray (n, 2)
In addition, redim can also define undefined arrays, such as redim myarray (10)



Number of B Arrays

The subscript specified when an array is defined by dim or redim indicates the maximum subscript allowed to access the array, but not the number of the array. In fact, the number of one-dimensional arrays is always equal to (the maximum subscript + 1). During access, the numbers are accessed one by one from 0 through the subscript.

For example, there are 6 array elements defined by dim myarray (5): myarray (0), myarray (1), myarray (2), and myarray (3), myarray (4), and myarray (5 ).

Another example: redim thisarray () actually defines a two-dimensional array (2 + 1) * (5 + 1) = 1 8.

In this case, it is not necessary to define an array with only one element? The answer is: no.

As mentioned above, the array defined by redim thisarray (1) actually has (1 + 1) array elements, but is similar to: redim
Thisarray (0) syntax, incorrect. Therefore, an array with only one array element cannot be defined.
In fact, the above is only about its default situation. In fact, to define an array, you can define the starting and ending numbers of the lower mark so as to define the number of arrays or even the starting and ending numbers of the lower mark. For example, redim
Thisarray (1980 to1990) defines an array containing 11 elements, subscript from 1980 to 1990.



C about ubound Functions

Ubound returns the maximum subscript of a one-dimensional array, rather than the number of elements. For example, dim
Myarray (5), then the value returned by ubound (myarray) is 5, not 6.
Ubound can also be applied to two-dimensional arrays. When applied to a two-dimensional array, it returns the maximum value of the first submark.

For example, dim myarray (6, 3 ),

Then the value returned by ubound (myarray) is 6, not 7, not 18 (6*3 = 18 ).

To return the maximum value of the second subobject, use ubound (myarray, 2 ).


Opposite to ubound is another function: lbound, which returns the minimum subscript of the array. Similar to ubound, lbound (myarray, 2) returns an array.
The minimum value of the second lower mark of myarray. Therefore, the number of elements in the one-dimensional array myarray is ubound (myarray)-lbound.
(Myarray) + 1, while the number of elements in a two-dimensional array is:

(Ubound (myarray)-lbound (myarray) + 1) * (ubound (myarray, 2)-lbound (myarray, 2) + 1)

Multi-dimensional array.

Array Definition

Program code

Dim myarray
Myarray = array (, 98)




Extensible Array

Program code

Dim myarray ()
For I = 0 to 10
Redim preserve myarray (I)
Myarray (I) = I
Next



An array that splits a string and returns the split result.

Program code

Dim myarray
Myarray = Split (tempcnt, CHR (13) & CHR (10 ))
For I = lbound (myarray) to ubound (myarray)
Response. Write myarray (I) & "<br>"
Next



Array sorting Function

Program code

Function sort (ary)
Keepchecking = true
Do until keepchecking = false
Keepchecking = false
For I = 0 to ubound (ary)
If I = ubound (ary) Then exit
If ary (I)> ary (I + 1) then
Firstvalue = ary (I)
Secondvalue = ary (I + 1)
Ary (I) = secondvalue
Ary (I + 1) = firstvalue
Keepchecking = true
End if
Next
Loop
Sort = ary
End Function



Application Example of array sorting Function

Program code

Dim myarray
Myarray = array (, 98)
Myarray = sort (myarray)
For I = lbound (myarray) to ubound (myarray)
Response. Write myarray (I) & "<br>"
Next



Use arrays in application and session

Program code

Application. Lock
Application ("storedarray") = myarray
Application. Unlock

Localarray = Application ("storedarray ")




Overwrite the array in Application

Program code

Application. Lock
Application ("storedarray") = localarray
Application. Unlock



The session usage is the same as that of the application.

Import data from the database into the Array



Program code

Dim myarray
Retrieve all records
Myarray = Rs. getrows
Retrieve the first 10 records
Myarray = Rs. getrows (10)

For ROW = 0 to ubound (myarray, 2)
For Col = 0 to ubound (myarray, 1)
Response. Write (COL, row) & "<br>"
Next
Next
Related Article

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.