定義一個(gè)簡(jiǎn)單的數(shù)組
有兩種方法在asp中定義數(shù)組,讓我們看看每種的例子:
方法一:
MyArray = Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct", "Nov","Dec")
方法二:
Dim myArray(2)
myArray(0)="Jan"
myArray(1)="Feb"
動(dòng)態(tài)改變數(shù)組的大小
DIM myArray()
REDIM myArray(20) '將數(shù)組重新定義為20維
可擴(kuò)展數(shù)組舉例:將數(shù)組從0擴(kuò)展到10
Dim MyArray()
for i = 0 to 10
ReDim Preserve MyArray(i) ‘Preserve 保留原來(lái)數(shù)據(jù)
MyArray(i)=...
next
.................................................................................................
有用的數(shù)組函數(shù)
Ubound(arrayName)函數(shù)
Lbound(arrayName)這個(gè)函數(shù)是返回?cái)?shù)組的上標(biāo),也就是數(shù)組第一個(gè)元素的標(biāo)記。
.................................................................................................
.................................................................................................
向另一個(gè)頁(yè)面?zhèn)鬟f數(shù)組
現(xiàn)在有很多種方法向另一頁(yè)面?zhèn)鬟f數(shù)組,目前有三種方法:
定義一個(gè)又逗號(hào)分隔的字符串,然后再下一頁(yè)中用Split函數(shù)重新建立數(shù)組。
將數(shù)組存儲(chǔ)在一個(gè)Session變量中,然后在下一個(gè)頁(yè)面中調(diào)用。
通過(guò)表單的隱含區(qū)域來(lái)傳遞數(shù)組,他們都是自動(dòng)用逗號(hào)分開(kāi),然后再用Split函數(shù)重新建立數(shù)組。
前兩種方法很好,但是都比第三中復(fù)雜。在這里我們將只介紹第三種,因?yàn)樗亲詈?jiǎn)單最有效的。
1.asp:
<%
dim I
dim myArray(20)
for I=0 to 20
myArray(I)="Item " & I
next
%>
<html>
<body>
<form name="testform" method="post" action="2.asp">
<%
for I=0 to ubound(myArray)
response.write "<input type=hidden name=myArray value='" & myArray(I) & "'>"
next
%>
<p>
<input type="submit">
</form>
</body>
</html>
以上我們做的是在一個(gè)表單中用單獨(dú)的隱含域存儲(chǔ)數(shù)組中的每個(gè)元素,我們?cè)倏纯聪乱豁?yè):
2.asp
<html>
<body>
<%
dim arrString
dim myArray
dim I
arrString=request("myArray")
myArray = split(arrString,",")
for I=0 to ubound(myArray)
response.write "Item "&I&" = " & myArray(I) & "<br>" & vbCrLf
next
%>
</body>
</html>
------------------------------------------------
將一個(gè)字符串分割并返回分割結(jié)果的數(shù)組
Dim MyArray
MyArray = Split(tempcnt,chr(13)&chr(10))
For I = Lbound(MyArray) to Ubound(MyArray)
Response.Write MyArray(I) & "<br>"
Next
數(shù)組排序函數(shù)
function..Sort(ary)
KeepChecking = TRUE
Do Until KeepChecking = FALSE
KeepChecking = FALSE
For I = 0 to UBound(ary)
If I = UBound(ary) Then Exit For
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..
數(shù)組排序函數(shù)應(yīng)用例子
Dim MyArray
MyArray = Array(1,5,123,12,98)
MyArray = Sort(MyArray)
For I = Lbound(MyArray) to Ubound(MyArray)
Response.Write MyArray(I) & "<br>"
Next
在A(yíng)pplication和Session中使用數(shù)組
Application.Lock
Application("StoredArray") = MyArray
Application.Unlock
LocalArray = Application("StoredArray")
覆蓋Application中的數(shù)組
Application.Lock
Application("StoredArray") = LocalArray
Application.Unlock
Session使用方法與Application相同
從數(shù)據(jù)庫(kù)中把數(shù)據(jù)導(dǎo)入數(shù)組中
Dim MyArray
取出全部記錄
MyArray = RS.GetRows
取出前10項(xiàng)記錄
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
--------------------
原文地址:https://website.pbottle.com/a-582.html