如何在 asp-classic 中获取数组中的值
How to get value in array in asp-classic
数组内容是这样的..
str = "penalty: 6, ord: 1, ot: 0, fe: 7, rs: 7"
数组可以3~10个视情况而定
这样怎么放变量?
aa = 6
bb = 1
cc = 0
dd = 7
ee = 7
我试过了,但它不再有效了。
str_arr=Split(str,",")
if_num=UBound(str_arr)
redim str_arr1(if_num)
For k=0 To eval(if_num)
str_arr1(k)=Split(str_arr(k),":")
name = str_arr1(k)(0)
num = str_arr1(k)(1)
Next
您需要的是Split()
VBScript 方法,并将结果存储在Dictionary 对象中。
我编写了一个通用函数,它采用原始字符串、主分隔符和子分隔符,其中 returns 这样一个 Dictionary 对象,您可以迭代以查看值:
Function DoubleSplit(rawValue, mainSeparator, subSeparator)
Dim oResult, arrMainParts, arrSubParts
Dim x, curMainPart
'using Dictionary as key/value collection
Set oResult = Server.CreateObject("Scripting.Dictionary")
'split raw value by the main separator
arrMainParts = Split(rawValue, mainSeparator)
'iterate over main parts, split each by the sub separator
For x=0 To UBound(arrMainParts)
curMainPart = arrMainParts(x)
arrSubParts = Split(curMainPart, subSeparator)
If UBound(arrSubParts)=1 Then
'adding to result only if there are both key and value
oResult(arrSubParts(0)) = arrSubParts(1)
End If
'prevent memory leaks
Erase arrSubParts
Next
'prevent memory leaks
Erase arrMainParts
'assign function return value
Set DoubleSplit = oResult
End Function
使用示例,使用问题中给出的数据:
Dim str, myDictionary, key
str = "penalty: 6, ord: 1, ot: 0, fe: 7, rs: 7"
Set myDictionary = DoubleSplit(str, ", ", ": ")
For Each key In myDictionary.Keys
Response.Write("Name is " & key & ", value is: " & myDictionary(key) & "<br />")
Next
'prevent memory leaks
Set myDictionary = Nothing
数组内容是这样的..
str = "penalty: 6, ord: 1, ot: 0, fe: 7, rs: 7"
数组可以3~10个视情况而定
这样怎么放变量?
aa = 6
bb = 1
cc = 0
dd = 7
ee = 7
我试过了,但它不再有效了。
str_arr=Split(str,",")
if_num=UBound(str_arr)
redim str_arr1(if_num)
For k=0 To eval(if_num)
str_arr1(k)=Split(str_arr(k),":")
name = str_arr1(k)(0)
num = str_arr1(k)(1)
Next
您需要的是Split()
VBScript 方法,并将结果存储在Dictionary 对象中。
我编写了一个通用函数,它采用原始字符串、主分隔符和子分隔符,其中 returns 这样一个 Dictionary 对象,您可以迭代以查看值:
Function DoubleSplit(rawValue, mainSeparator, subSeparator)
Dim oResult, arrMainParts, arrSubParts
Dim x, curMainPart
'using Dictionary as key/value collection
Set oResult = Server.CreateObject("Scripting.Dictionary")
'split raw value by the main separator
arrMainParts = Split(rawValue, mainSeparator)
'iterate over main parts, split each by the sub separator
For x=0 To UBound(arrMainParts)
curMainPart = arrMainParts(x)
arrSubParts = Split(curMainPart, subSeparator)
If UBound(arrSubParts)=1 Then
'adding to result only if there are both key and value
oResult(arrSubParts(0)) = arrSubParts(1)
End If
'prevent memory leaks
Erase arrSubParts
Next
'prevent memory leaks
Erase arrMainParts
'assign function return value
Set DoubleSplit = oResult
End Function
使用示例,使用问题中给出的数据:
Dim str, myDictionary, key
str = "penalty: 6, ord: 1, ot: 0, fe: 7, rs: 7"
Set myDictionary = DoubleSplit(str, ", ", ": ")
For Each key In myDictionary.Keys
Response.Write("Name is " & key & ", value is: " & myDictionary(key) & "<br />")
Next
'prevent memory leaks
Set myDictionary = Nothing