n级嵌套循环vbs

时间:2016-06-08 14:27:40

标签: loops dynamic vbscript nested

有没有办法优化这个嵌套循环示例以使用动态数量的这些var_Arrays?

'Dim var0_Arr : var0_Arr = Array(min,   max,    step)
Dim var1_Arr : var1_Arr =   Array(0,    1.0,    0.5)
Dim var2_Arr : var2_Arr =   Array(0,    1.0,    0.5)
Dim var3_Arr : var3_Arr =   Array(0,    1.0,    0.5)
Dim var4_Arr : var4_Arr =   Array(0,    1.0,    0.5)
'Dim var5_Arr : var5_Arr = ...
'Dim var6_Arr : var6_Arr = ...
'...

for var1 = var1_Arr(0) to var1_Arr(1)  step var1_Arr(2)
    for var2 = var2_Arr(0) to var2_Arr(1)  step var2_Arr(2)
        for var3 = var3_Arr(0) to var3_Arr(1)  step var3_Arr(2)
            for var4 = var4_Arr(0) to var4_Arr(1)  step var4_Arr(2)
                msgbox var1 & " " & var2 & "    " & var3 & "    " & var4
            next
        next
    next
next

我想用这样的东西:

Dim var_Arr : var_Arr = Array(  Array(0,    1.0,    0.5),_
                                Array(0,    1.0,    0.5),_
                                Array(0,    1.0,    0.5),_
                                Array(0,    1.0,    0.5))

使用UBound(var_Arr)我会知道我有多深入嵌套循环,但我不知道如何结合动态范围(min,max,step)实现动态嵌套循环

1 个答案:

答案 0 :(得分:0)

这个答案解决了我的问题: https://stackoverflow.com/a/4683760/6440754

vbs中不支持Slice:

Function Slice(ByRef arrArray, intStart, intEnd)

    If intEnd = vbNull Then intEnd = UBound(arrArray)
    If intStart < 0 Then intStart = UBound(arrArray) - intStart

    arrTemp = Array()
    intCount = 0

    For i = intStart To intEnd
        ReDim Preserve arrTemp(intCount)
        arrTemp(intCount) = arrArray(i)
        intCount = intCount + 1
    Next

    Slice = arrTemp

End Function

来源https://www.aspfree.com/c/a/code-examples/creating-useful-array-functions/