如何使用VBScript将数组添加到数组中

时间:2012-05-24 20:48:24

标签: arrays vbscript

更新:这就是结果数据的样子:

arrComm rows 0-6
454-45-45   45345-3454   FIN  Elected   Finance Committee  Details  More Details
343-343-3   343-944244   COM  Appointed Comm Committee     Details  More Details

arrComm added row 7 that has array of all members of that committee
This below array is named newMembers and 1 array for each row is added to the 
arrComm with this code:  arrComm(j,7) = newMembers

454-45-45   John   S   Smith  Chair   2003   2005
454-45-45   Sara   T   Jones  VP      2012   2013
454-45-45   Tim    R   Jane   Pres    2011   2014

我编写了代码,为VBScript中现有数组的每个“行”添加一个数组。我没有错误,但我无法弄清楚我是否正确地做到了。每当我尝试从我放置新阵列的列中访问数据时,我什么也得不到。有人能告诉我在Vbscript中将数组添加到现有数组的正确语法吗?

现在,我循环遍历每一行并将我的数组添加到最后。像这样:


ReDim Preserve arrComm(UBound(arrComm), 7)

'Loop through Committee Array and add Members Array 
Dim newMembers()

For j = 0 to UBound(arrComm)
'create a new array out of Members for each committee
 cmt_key = arrComm(j,0)

     'First find count of Members array that matches committee  
     y=0
     For s = 0 to UBound(arrMembers)
        If arrMembers(s,0) = cmt_key Then
        y=y+1
        End If  
     Next


     'Next build new array only with those members that match committee

     Redim newMembers(y,8)

     'Now loop through Original Members Array and add to New Members Array
     y=0
     For s = 0 to UBound(arrMembers)
        If arrMembers(s,0) = cmt_key Then
            newMembers(y,0) = arrMembers(s,0)
            newMembers(y,1) = arrMembers(s,1)
            newMembers(y,2) = arrMembers(s,2)
            newMembers(y,3) = arrMembers(s,3)
            newMembers(y,4) = arrMembers(s,4)
            newMembers(y,5) = arrMembers(s,5)
            newMembers(y,6) = arrMembers(s,6)
            newMembers(y,7) = arrMembers(s,7)
            newMembers(y,8) = arrMembers(s,8)
            y=y+1


        End If  
     Next



     'Now finally add newly created members array to Committee array

     arrComm(j,7) = newMembers(y,8)



Next

更新: 让我试着更好地解释我的上述代码。 1. ReDim Preserve arComm ...获取我现有的7列数组,并添加第8列。 UBound(arrComm)确保将其设置为正确的现有行数。

  1. 接下来,我创建一个名为newMembers

  2. 的新数组
  3. 然后我必须指定newMembers的长度,所以我遍历数组我从(arrMembers)中拉出我的新成员,并从arrMembers中获取将被放入newMembers的数量。 (y)的

  4. 然后,一旦我有了这个数,我用(y)和9列重新开始新成员

  5. 然后我浏览arrMembers并将符合我标准的金额提取到newMembers

  6. 然后我将名为newMembers的新创建的数组添加到arrComm数组的最后一列。

  7. 然后我圈起来,转到arrComm数组的下一行并重新开始,创建另一个newMembers数组,这次不同的行数,然后最后将它添加到arrComm数组的下一行。并且一遍又一遍,直到我为每一行的arrComm的最后一列添加了一个新数组。

2 个答案:

答案 0 :(得分:1)

我不了解您的代码,但是如果您想要增加数组,则必须使用ReDim 保留来保留以前的内容,而ReDim Preserve只允许增加最后一个维度。

WRT评论:

此:

Option Explicit

Dim i
For i = 0 To 3
    ReDim Preserve a(0, i)
    WScript.Echo i, "growing to", UBound(a, 2)
    a(0, i) = Array(i, "a", "b")
Next
WScript.Echo "--------"
For i = 0 To UBound(a, 2)
    WScript.Echo i, "the array", Join(a(0, i))
Next

输出:

cscript 00.vbs
0 growing to 0
1 growing to 1
2 growing to 2
3 growing to 3
--------
0 the array 0 a b
1 the array 1 a b
2 the array 2 a b
3 the array 3 a b

演示如何生成数组并将另一个数组存储到不断增长的(last!)维度中。

请标记

之间的区别
ReDim Preserve arrComm(UBound(arrComm), 7) 

ReDim Preserve a(0, i)

答案 1 :(得分:1)

如果没有提供的测试数据,很难排除故障,但按照你所说的,你想“向现有数组的每一行”添加一个数组“,这不是我在你的代码中看到的情况。实际上,您将一个数组的元素添加为另一个数组的元素。如果你想添加整个数组,我想你会这样做,你会做类似的事情:

arrComm(j,7) = newMembers

因为你想要那里的所有成员,对吧,而不仅仅是最后一个成员? (更不用说你在循环中增加了y,所以事实上你甚至没有得到最后一个。)然后,当然,在你的输出代码中,你必须在newMembers中查询ubounds,因为它是动态创建的。

这种情况之一是复杂性几乎要求您将代码分解为具有虚拟数据的独立可运行页面以进行测试,尤其是帮助我们查看问题。

添加示例代码以转储结果数组:

dim tempMembers
' pick a Members element for testing
tempMembers = arrComm(0,7)
' dump the array
dim a,b
for a = 0 to ubound(tempMembers,1)
    for b = 0 to ubound(tempMembers,2)
        response.write (tempMembers(a,b) & ",")
    next
    response.write "<br />"
next

我们真正需要的是一些入站数据示例。我能够使用以下代码运行代码,但显然它不能反映现实:

dim arrMembers()
dim arrComm()
ReDim Preserve arrComm (1,1)
ReDim Preserve arrMembers (1,8)
arrComm (0,0) = "target"
arrMembers (0,0) = "target"

你可以将其充实以反映真实数据的样子吗?填充所有尺寸的一行或两行就足够了。

基于部分测试数据的完整工作示例:

dim arrMembers()
dim arrComm()
ReDim Preserve arrComm (2,7)
ReDim Preserve arrMembers (2,8)
arrComm (0,0) = "454-45-45"
arrComm (0,1) = "45345-3454"
arrComm (0,2) = "FIN"
arrComm (0,3) = "Elected"
arrComm (0,4) = "Finance Committee"
arrComm (0,5) = "Details"
arrComm (0,6) = "More Details"
arrComm (1,0) = "343-343-3"
arrComm (1,1) = "343-944244"
arrComm (1,2) = "COM"
arrComm (1,3) = "Appointed"
arrComm (1,4) = "Comm Committee"
arrComm (1,5) = "Details"
arrComm (1,6) = "More Details"
'
arrMembers (0,0) = "454-45-45"
arrMembers (0,1) = "John"
arrMembers (0,2) = "S"
arrMembers (0,3) = "Smith"
arrMembers (0,4) = "Chair"
arrMembers (0,5) = "2003"
arrMembers (0,6) = "2005"
'
arrMembers (1,0) = "454-45-45"
arrMembers (1,1) = "Sara"
arrMembers (1,2) = "T"
arrMembers (1,3) = "Jones"
arrMembers (1,4) = "Chair"
arrMembers (1,5) = "2012"
arrMembers (1,6) = "2013"
' non-match member
arrMembers (2,0) = "343-343-3"
arrMembers (2,1) = "Joe"
arrMembers (2,2) = "T"
arrMembers (2,3) = "Schmoe"
arrMembers (2,4) = "Chair"
arrMembers (2,5) = "2012"
arrMembers (2,6) = "2013"

ReDim Preserve arrComm(UBound(arrComm), 7)

'Loop through Committee Array and add Members Array 
Dim newMembers()

For j = 0 to UBound(arrComm)
'create a new array out of Members for each committee
 cmt_key = arrComm(j,0)

     'First find count of Members array that matches committee  
     y=0
     For s = 0 to UBound(arrMembers)
        If arrMembers(s,0) = cmt_key Then
        y=y+1
        End If  
     Next


     'Next build new array only with those members that match committee

     Redim newMembers(y,8)

     'Now loop through Original Members Array and add to New Members Array
     y=0
     For s = 0 to UBound(arrMembers)
        If arrMembers(s,0) = cmt_key Then
            newMembers(y,0) = arrMembers(s,0)
            newMembers(y,1) = arrMembers(s,1)
            newMembers(y,2) = arrMembers(s,2)
            newMembers(y,3) = arrMembers(s,3)
            newMembers(y,4) = arrMembers(s,4)
            newMembers(y,5) = arrMembers(s,5)
            newMembers(y,6) = arrMembers(s,6)
            newMembers(y,7) = arrMembers(s,7)
            newMembers(y,8) = arrMembers(s,8)
            y=y+1


        End If  
     Next



     'Now finally add newly created members array to Committee array

     arrComm(j,7) = newMembers



Next
'response.write (arrComm(0,7)(0,0))
dim tempMembers
' pick a Members element for testing
tempMembers = arrComm(0,7)
response.write (ubound(tempMembers,1) & " | " & ubound(tempMembers,2) & "<br />")
' dump the array
dim a,b
for a = 0 to ubound(tempMembers,1)-1
    for b = 0 to ubound(tempMembers,2)-1
        response.write (tempMembers(a,b) & ",")
    next
    response.write "<br />"
next