asp数组笔记

ASP   2025-02-26 18:07   65   0  
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
Dim arr()
ReDim arr(4)
For i = 0 To 4
    arr(i) = "id:"&i ' 填充数组元素
	Response.Write(arr(i)) ' 输出第一个元素
Next
'Response.Write(arr(0)) ' 输出第一个元素


'ASP获取数组长度
Dim arrlen(3)
arrlen(0) = "Apple"
arrlen(1) = "Banana"
arrlen(2) = "Cherry"
Dim length
length = UBound(arrlen) - LBound(arrlen) + 1
'Response.Write("数组长度:" & length)
'response.Write(ArrayLength(arrlen))


'将字符串分割为数组
'Dim myString
'myString = "Value1,Value2,Value3"
'Dim myArray
'myArray = Split(myString, ",")

'使用 UBound 函数(适用于一维数组)
Dim arrx(5) ' 声明一个有 6 个元素的一维数组,下标从 0 到 5
'Response.Write "数组长度为:" & UBound(arrx, 1) + 1 ' 输出数组长度为 6


'遍历数组元素统计长度(适用于任意维度数组)
Dim multiArrayx(2, 3) ' 声明一个二维数组
multiArrayx(0, 0) = "A"
multiArrayx(0, 1) = "B"
multiArrayx(0, 2) = "C"
multiArrayx(1, 0) = "D"
multiArrayx(1, 1) = "E"
multiArrayx(1, 2) = "F"
Dim iy, jy, county
county = 0
For iy = 0 To UBound(multiArrayx, 1)
    For jy = 0 To UBound(multiArrayx, 2)
        county = county + 1
    Next
Next
'Response.Write "数组元素总个数为:" & county ' 输出数组元素总个数为 8


'使用 Scripting.Dictionary 对象(适用于键值对集合)
Set dictx = CreateObject("Scripting.Dictionary")
dictx.Add "key1", "value1"
dictx.Add "key2", "value2"
dictx.Add "key3", "value3"
'Response.Write "字典中的键值对数量为:" & dictx.Count ' 输出字典中的键值对数量为 3


'数组基础操作
dim shuzu,i
shuzu = Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
shuzu = Array("jieguo")	'给数组shuzu追加一个元素
'For i=0 To ubound(shuzu)
    'Response.Write(shuzu(i)&"<br/>")
'Next


'redim shuzu(20)	'将数组shuzu清空重新定义20个元素大小
redim preserve shuzu(20)	'将数组shuzu数据保留并重新定义元素大小
shuzu(1) = "yiyue"
shuzu(2) = "eryue"


'数组赋值为数组并循环操作
Dim arrr(3), i1, i2
arrr(0) = Array("id:100", "age:101")
arrr(1) = Array("id:200", "age:202")
arrr(2) = Array("id:300", "age:303")
' 循环读取数组
For i1 = 0 To UBound(arrr)
    For i2 = 0 To UBound(arrr(i1))
        Response.Write arrr(i1)(i2) & "<br>"
    Next
Next


response.End()

'定义数组
'Dim myArray
'Set myArray = Server.CreateObject("Scripting.Dictionary")

'Dim myArray
'myArray = Array()

'Dim myArray
'myArray = Empty

'Dim myArray
'Set myArray = Server.CreateObject("Scripting.Dictionary")
'或者
'myArray = Array()

'使用Dim语句
Dim arr2(1, 2)
arr2(0, 0) = "A"
arr2(0, 1) = "B"
arr2(0, 2) = "C"
arr2(1, 0) = "D"
arr2(1, 1) = "E"
arr2(1, 2) = "F"
Response.Write("Array values: ")
For i = 0 To 1
    For j = 0 To 2
        Response.Write(arr2(i, j) & " ")
    Next
Next
Response.Write("<br>")

'使用Redim语句动态定义大
Dim arr3()
ReDim arr3(1, 2)
arr3(0, 0) = "A"
arr3(0, 1) = "B"
arr3(0, 2) = "C"
arr3(1, 0) = "D"
arr3(1, 1) = "E"
arr3(1, 2) = "F"
Response.Write("Array values: ")
For i = 0 To 1
    For j = 0 To 2
        Response.Write(arr3(i, j) & " ")
    Next
Next
Response.Write("<br>")


Dim scriptdic
Set scriptdic = Server.CreateObject("Scripting.Dictionary")
scriptdic.Add "bj", "beijing"  ' 正确使用键值对
scriptdic.Add "sh", "shanghai"
scriptdic.Add "gz", "guangzhou"
'response.Write(scriptdic.count)
'response.Write(scriptdic.Exists("bj"))
'response.Write(scriptdic.Remove("bj"))
'response.Write(scriptdic.item("bj"))
'scriptdic.RemoveAll
'KeyArr = scriptdic.keys '返回所有键到数组KeyArr
'ItemArr = scriptdic.items '返回所有项到数组ItemArr

'arrKeys = scriptdic.Keys
'arrItems = scriptdic.Items
'for m=0 to ubound(arrKeys)
'	response.Write(arrKeys(m))
'next
'for m=0 to ubound(arrItems)
'	response.Write(arrItems(m))
'next

'For i=1 To scriptdic.count
'	'response.Write(i)
'	arrKeys = scriptdic.Keys
'	for m=0 to ubound(arrKeys)
'		response.Write(arrKeys(m))
'	next
'	Response.Write(scriptdic(i)&"<br/>")
'Next

'arrKeys = scriptdic.Keys	'获取数组scriptdic中的所有key键
'arrItems = scriptdic.Items	'获取数组scriptdic中的所有item值

'For intLoop = 0 To scriptdic.Count –1
	'StrThisKey = arrKeys(intLoop)
	'StrThisItem = arrItems(intLoop)
'Next

'var arrKeys = new VBArray(scriptdic.Keys()).toArray();
'var arrItems = new VBArray(scriptdic.Items()).toArray();
'for (intLoop = 0; intLoop < scriptdic.Count; intLoop++) {
'    strThisKey = arrKeys[intLoop];
'	strThisItem = arrItems[intLoop];
'}

'For Each objItem in arrItems
'Response.Write objItem & " = " & arrItems(objItem) & "<BR>"
'Next

response.End()

For Each item In shuzu
    Response.Write(item & "<br>")
Next

Dim counter, thismonth
counter = 1
thismonth = Month(Now())
Do Until counter = thismonth
    'Response.Write(counter & " month : "&thismonth)
    'Response.Write("<br>")
    counter = counter + 1
Loop

response.Write("<br/>")

Dim counter2, thismonth2
counter2 = 1
thismonth2 = Month(Now())
Do While counter2 < thismonth2
    Response.Write(counter2 & " month : "&thismonth2)
    'Response.Write("<br>")
    counter2 = counter2 + 1
Loop


'asp空数组使用for语句循环赋值
'使用Scripting.Dictionary的示例:
'Dim arr
'Set arr = CreateObject("Scripting.Dictionary")
'For i = 1 To 5
'    arr.Add i, "" ' 使用Add方法添加元素,键和值可以是任何类型
'Next
'Response.Write(arr(1)) ' 输出第一个元素

'使用Redim的示例:
'Dim arr()
'ReDim arr(4) ' 创建一个有5个元素的数组,索引从0到4
'For i = 0 To 4
'    arr(i) = "" ' 填充数组元素
'Next
'Response.Write(arr(0)) ' 输出第一个元素


'ASP获取数组长度函数
function ArrayLength(arry)
	dim len
	len=0
	for each val in arry
		if val<>"" then
			len=len+1
		end if 
	next
	ArrayLength=len
end function









%>


博客评论
还没有人评论,赶紧抢个沙发~
发表评论
说明:请文明发言,共建和谐网络,您的个人信息不会被公开显示。