3

My pages are ASP with VBScript. I have two different recordsets and each has it own array (by using RS.GetRows() method). Basically, I want to combine these two arrays into one array. Please see my demo below.

Please help, thank you....

Dim array1
array1(0,0)="a"
array1(0,1)="b"
array1(1,0)="c"
array1(1,1)="d"

Dim array2
array2(0,0)="e"
array2(0,1)="f"
array2(1,0)="g"
array2(1,1)="h"

/////  I want to combine into one array something like this. //////
Dim array1
array1(0,0)="a"
array1(0,1)="b"
array1(1,0)="c"
array1(1,1)="d"
array1(0,2)="e"
array1(0,3)="f"
array1(1,2)="g"
array1(1,3)="h"

/////  Basically, same columm numbers, but adding more data/rows to the existing array. 
OR if this is not doable, is there a way to combine these arrays into a brand new array?


/////  I have this much so far, but couldn't get it to work ///////
For i = 0 to Ubound(array2, 2)
    Redim Preserve array1(i, 1)
    array1(i, 0) = array2(0, i)
    array1(i, 1) = array2(1, i)
Next

2 Answers 2

8

Dynamic VBScript arrays can be enlarged only in/for the last dimension. That is why GetRows() returns an array with the 'growable' rows in the last dimension.

So your

Redim Preserve array1(i, 1)  ' trying to grow the first dimension

will not work.

This 'works':

Option Explicit

Dim array1(1, 1)
array1(0,0)="a"
array1(0,1)="b"
array1(1,0)="c"
array1(1,1)="d"
dispArr "array1", array1

Dim array2(1, 1)
array2(0,0)="e"
array2(0,1)="f"
array2(1,0)="g"
array2(1,1)="h"
dispArr "array2", array2

Dim array3 : array3 = mergeArr( array1, array2)
dispArr "array3", array3

Sub dispArr(t, a)
  WScript.Echo "Array:", t
  Dim i, j
  For i = 0 To UBound(a, 2)
      For j = 0 To UBound(a, 1)
          WScript.Stdout.Write a(j, i) & " "
      Next
      WScript.Echo
  Next
End Sub

Function mergeArr(a1, a2)
  ReDim aTmp(Ubound(a1, 1), UBound(a1, 2) + Ubound(a2, 2) + 1)
  Dim i, j, k
  For i = 0 To UBound(a1, 2)
      For j = 0 To UBound(aTmp, 1)
          aTmp(j, i) = a1(j, i)
      Next
  Next
  For k = 0 To UBound(a2, 2)
      For j = 0 To UBound(aTmp, 1)
          aTmp(j, i + k) = a2(j, k)
      Next
  Next
  mergeArr = aTmp
End Function

output:

cscript 22029206.vbs
Array: array1
a c
b d
Array: array2
e g
f h
Array: array3
a c
b d
e g
f h

but you may not like the row/col layout/order.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. I will try to follow your method and see how it comes out for the row/column.
4

I wouldn't use this method with too many records, as strings in VBScript are notoriously slow but you could just append the recordset strings. For example:

' Combine both recordsets and split into a row array...
a = Split(rs1.GetString() & rs2.GetString(), vbCr)

' Now make a sub-array for the columns of each row...
For i = 0 To UBound(a)
    a(i) = Split(a(i), vbTab)
Next

' Now you can address each row and column like this: a(row)(column)

6 Comments

Thanks @Bond, your solution is actually works better (less codes). I try to understand how it works, but I am still confused. So GetString() automatically put the data with Carriage-Return seperator, then we have to use SPLIT to split them? Next, I am totally lost where SPLIT with vbTab. Please explain if you don't mind.
Sure. GetString() returns the recordset as a string. It uses a carriage return (vbCr) to separate rows and a tab (vbTab) to separate columns. So the first thing we do is split by vbCr to get each row. Now a(0) is the first record, a(1) is the second record, etc. But each array element is still a single string that contains all the column values. So we need to split each of these strings, too, so that we have an array of column values. In the end, we have a row array where each element contains a column array. Hope that makes sense.
Thanks again, it makes sense now. I guess when I see "rs1.GetString() & rs2.GetString()", I thought it combines horizontally like this "rs1.Col1 + vbTab + rs1.Col2 + vbCr + r2.Col1 + vbTab + rs2.Col2 + vbCr"... so on
Cool. Glad it made sense. After rs1.GetString() & rs2.GetString() it should look like this: rs1.r1c1 & vbTab & rs1.r1c2 & vbCr & rs1.r2c1 & vbTab & rs1.r2c2 & vbCr & rs2.r1c1 & vbTab & rs2.r1c2 & vbCr & rs2.r2c1 & vbTab & rs2.r2c2 & vbCr
Won't this break if you have carriage returns or tabs in your data?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.