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
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With