基于变量的循环数组

时间:2012-12-02 17:34:46

标签: arrays vbscript

我需要基于变量Y创建一个特定的数组(我在一系列中使用)。这是我正在使用的格式 - 尝试在VBScript中进行。

If Y=2 then

    S1 = [0,x]
    S2 = [x,0]

If Y=3 then

    S1 = [0,0,x]
    S2 = [0,x,0]
    S3 = [x,0,0]

If Y=4 then

    S1 = [0,0,0,x]<br/>
    S2 = [0,0,x,0]<br/>
    S3 = [0,x,0,0]<br/>
    S4 = [x,0,0,0]<br/>

If Y=5 then

    S1 = [0,0,0,0,x]
    S2 = [0,0,0,x,0]
    S3 = [0,0,x,0,0]
    S4 = [0,x,0,0,0]
    S5 = [x,0,0,0,0]

等等我知道这将是一个for循环-------------

for i = 1 to Y
    S[i] = "[.... this is where am drawing a brain freeze
next

1 个答案:

答案 0 :(得分:0)

您可以使用arraylists动态创建数组并将它们传输回vbscript数组,以使其可以访问其他函数。 (或者你可以继续使用我认为优于普通数组的arraylists。)

Option Explicit

' Initialize
Dim x : x = "X"
Dim Y : Y = 5
Dim AL : Set AL = CreateObject("System.Collections.ArrayList")
Dim i, j, innerAL, S

' Make a loop to iterate over the amount of array entries we have to create
for i = 1 to Y

    ' This arraylist is used to set the inner array
    Set innerAL = CreateObject("System.Collections.ArrayList") 

    ' Fill the inner array
    for j = 0 to Y-1

        ' See if it has to be filled with X or 0
        If j = (Y-i) then
            innerAL.Add x
        else
            innerAL.Add 0
        end if
    next

    ' Convert the inner arraylist to a standard array and add it to the outer arraylist
    AL.Add innerAL.ToArray()

next

' We are done. Convert the arraylist to a standard array
S = AL.ToArray()

' Display the results
for i = 0 to Y-1
    wscript.echo "S(" & i & ") = [" & join(S(i), ",") & "]"
Next