asp數(shù)組的使用介紹
定義簡單數(shù)組
有兩種方法在asp中定義和初始化數(shù)組,讓我們看看每種的例子:
方法一:
MyArray = Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct", "Nov","Dec")
數(shù)組大小由初始化元素個數(shù)決定。
方法二:
Dim myArray(2) '指定數(shù)組大小
myArray(0)="Jan"
myArray(1)="Feb"
數(shù)組動態(tài)擴展
DIM myArray()
REDIM myArray(20) '將數(shù)組大小重新定義為20ReDim Preserve MyArray(i) 'Preserve 保留數(shù)組中的原有數(shù)據(jù)
二維數(shù)組
舉例:
dim MyArray(5,10) '定義了一個二維數(shù)組
二維賦值舉例:
MYArray(3,3)=100
二維數(shù)組還有一種變相的實現(xiàn)方法:
dim MyArray(5)
MyArray(0)=Array(...) '一維數(shù)組
MyArray(1)=Array(...)'一維數(shù)組
...
訪問的時候,用MyArray(x)(y)這樣的格式
數(shù)組的下標
用上面的方法定義數(shù)組,每一維數(shù)組的第一個元素的下標是0,最后一個元素的下標就是元素數(shù)量-1
但也可以指定數(shù)組的下標,如:
dim MyArray1(3 to 10) '下標從3到10,MyArray(3)即獲取第一個元素的值
有用的數(shù)組函數(shù)
Ubound(數(shù)組名)函數(shù)--返回數(shù)組的最后一個元素的下標。
Lbound(數(shù)組名)函數(shù)--返回數(shù)組的第一個元素的下標,缺省為0。
更多應用:
數(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ù)應用例子
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
將一個字符串分割并返回數(shù)組
Dim MyArray MyArray = Split(字符串,分割符) For I = Lbound(MyArray) to Ubound(MyArray) Response.Write MyArray(I) & "<br>" Next
在Application和Session中使用數(shù)組
Application.Lock
Application("StoredArray") = MyArray
Application.UnlockLocalArray = Application("StoredArray")
覆蓋Application中的數(shù)組
Application.Lock
Application("StoredArray") = LocalArray
Application.Unlock
Session使用方法與Application相同
從數(shù)據(jù)庫中把數(shù)據(jù)導入數(shù)組中
Dim MyArray
取出全部記錄
MyArray = RS.GetRows
取出前10項記錄
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
向另一個頁面?zhèn)鬟f數(shù)組
現(xiàn)在有很多種方法向另一頁面?zhèn)鬟f數(shù)組,目前有三種方法:
定義一個又逗號分隔的字符串,然后再下一頁中用Split函數(shù)重新建立數(shù)組。
將數(shù)組存儲在一個Session變量中,然后在下一個頁面中調(diào)用。
通過表單的隱含區(qū)域來傳遞數(shù)組,他們都是自動用逗號分開,然后再用Split函數(shù)重新建立數(shù)組。
前兩種方法很好,但是都比第三中復雜。在這里我們將只介紹第三種,因為它是最簡單最有效的。
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>
以上我們做的是在一個表單中用單獨的隱含域存儲數(shù)組中的每個元素,我們再看看下一頁:
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>
以上就是asp數(shù)組的使用介紹的詳細內(nèi)容,更多關(guān)于asp數(shù)組使用的資料請關(guān)注腳本之家其它相關(guān)文章!
- asp取得數(shù)組中的最大值的方法
- asp下使用數(shù)組存放數(shù)據(jù)的代碼
- asp 得到動態(tài)數(shù)組中元素的個數(shù)
- asp.net 數(shù)組中字符串替換的幾種方式
- asp 動態(tài)數(shù)組 提供Add、Insert、Remove、RemoveAt、Search等方法。
- asp.net 字符串、二進制、編碼數(shù)組轉(zhuǎn)換函數(shù)
- asp.net通過js實現(xiàn)Cookie創(chuàng)建以及清除Cookie數(shù)組的代碼
- asp textarea 多行數(shù)組分割處理方法
- asp 數(shù)組 重復刪除函數(shù)(腳本之家增強版)
- ASP 過濾數(shù)組重復數(shù)據(jù)函數(shù)(加強版)
- ASP 使用Filter函數(shù)來檢索數(shù)組的實現(xiàn)代碼
- Asp與JS的數(shù)組和字符串下標介紹
- asp中使用redim、preserve創(chuàng)建動態(tài)數(shù)組實例
- ASP定義數(shù)組方法的技巧
相關(guān)文章
asp base64 utf-8為了兼容asp.net的base64
asp base64 utf-8為了兼容asp.net的base642009-07-07ASP操作Excel相關(guān)技術(shù)總結(jié)
ASP操作Excel相關(guān)技術(shù)總結(jié)...2007-05-05