Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Update: This is what resulting data should look like:

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

I've written code to add an array to each "row" of an existing array in VBScript. I doesn't error out, but I can't figure out if I'm doing it correctly. Anytime I try to access data from the column that I placed the new array, I get nothing. Can someone show me the correct syntax for adding an array to an existing array in Vbscript?

Right now, I am looping through each row and adding my array to the end. Like this:


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

Update: Let me try to better explain my above code. 1. ReDim Preserve arComm... takes my existing array of 7 columns and add's an 8th column to it. UBound(arrComm) makes sure that it sets it to the correct amount of existing rows.

  1. Next I create a new array called newMembers

  2. Then I have to specify the length of newMembers so I loop through the array I am pulling my newMembers from (arrMembers) and get a count of the amount from arrMembers that will be placed into newMembers. (y)

  3. Then once I have this count, I Redim newMembers with (y) and 9 columns

  4. Then I go through arrMembers and pull the amount that matches my criteria into newMembers

  5. Then I add my newly created array called newMembers to the last column of the arrComm array.

  6. Then I circle back up, go to the next row of the arrComm array and start over, creating another newMembers array, this time different amount of rows, and then finally adding it to the next row of the arrComm array. And over and over until I have added a new array to the last column of arrComm for every single row.

share|improve this question
add comment (requires an account with 50 reputation)

2 Answers

up vote 1 down vote accepted

It's hard to troubleshoot without test data provided, but going by what you're saying, that you want to "add an array to each "row" of an existing array", this is not what I see happening in your code. In fact, you're adding an element of one array as an element of another. If you want to add the whole array, and I think you do, you'd do something like:

arrComm(j,7) = newMembers

Because you want all the members in there, right, not just the last one? (Not to mention you've post-incremented y in the loop, so in fact you're not even getting the last one.) And then, of course, in your output code you'd have to interrogate the ubounds within newMembers, since it's dynamically-created.

This is one of those situations where the complexity almost requires you to break the code off into a standalone runable page with dummy data in order to test, and especially to help us see the problem.

Adding sample code to dump resulting array:

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

What we really need is some sample inbound data. I was able to get the code to run with the following, but obviously it doesn't reflect reality:

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

Could you flesh that out to reflect what real data looks like? One or two rows with all the dimensions filled would be enough.

Full working example based on some of your test data:

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
share|improve this answer
Rich, I tried your suggestion, which seems right to me and I didn't get any errors, however when I try to do the following code to test for results, I get an out of range error.. For w = 0 to UBound(arrComm) Response.Write("New array: " & arrComm(w,7)(1) & "<BR>") Next – trevoray May 25 '12 at 0:17
See my edit above. Not readable as a comment. – Rich May 25 '12 at 0:30
Rich, I made an edit that includes what final data is supposed to look like. I'm pretty positive that my final array is correctly put together, I just can't figure out how to write the contents of my final array. Especially the contents of my last row. – trevoray May 25 '12 at 11:58
Rich, I just understood your code. I didn't realize you made it so easy all I'd have to do is cut and paste. :) Your code does return the array that is placed in the last column of row 0 of my arrComm array. Thanks! – trevoray May 25 '12 at 12:12
Rich, just so I'm clear, is the first ubound(tempMembers,1) the amount of members in the array and the second ubound(tempMembers,2) is the amount of fields in the array? – trevoray May 25 '12 at 12:33
show 1 more commentadd comment (requires an account with 50 reputation)

I don't understand your code, but if you want to grow an array, you must use ReDim Preserve to keep the previous content and ReDim Preserve allows growing of the last dimension only.

WRT comments:

This:

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

output:

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

demonstrates how to grow an array and store another array into the growing (last!) dimension.

Please mark the difference between

ReDim Preserve arrComm(UBound(arrComm), 7) 

and

ReDim Preserve a(0, i)
share|improve this answer
yes, i did a redim preserve right before this code: ReDim Preserve arrComm(UBound(arrComm), 7) – trevoray May 24 '12 at 21:36
look at the bottom line. does that look like correct syntax for adding an array to the last "column" of an existing array? – trevoray May 24 '12 at 21:37
ok, well according to your example, it looks like i did my code correctly. although nothing displays when i try to write out the content of the last column. – trevoray May 24 '12 at 22:02
@trevoray I did a further update of my answer – Ekkehard.Horner May 24 '12 at 22:06
@Ekkehard.Homer, I did an update to my question. – trevoray May 24 '12 at 22:20
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.