如何在asp-classic中获取数组值

时间:2017-11-14 16:41:31

标签: arrays 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

1 个答案:

答案 0 :(得分:2)

您需要的是Split() VBScript方法,并将结果存储在Dictionary对象中。

我编写了一个泛型函数,它接受一个返回这样一个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