我的网页是使用VBScript的ASP。我有两个不同的记录集,每个记录集都有自己的数组(通过使用RS.GetRows()方法)。基本上,我想将这两个数组合并为一个数组。请参阅下面的演示。
请帮助,谢谢....
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
答案 0 :(得分:8)
动态VBScript数组只能在/最后一个维度上放大。这就是为什么GetRows()返回一个在最后一个维度中具有“可增长”行的数组。
所以你的
Redim Preserve array1(i, 1) ' trying to grow the first dimension
无效。
这'有用':
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
输出:
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
但您可能不喜欢行/列布局/顺序。
答案 1 :(得分:4)
我不会将此方法与太多许多记录一起使用,因为VBScript中的字符串非常慢,但您可以只追加记录集字符串。例如:
' 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)