Hi,
im confused about realisation of following problem (VB6 compiler):
There re two one - dimensioned Arrays,
a_1(x1) and a_2(x2).
Now i have got a third array which is doubledimensined.
a_3(x3, x4) where is intended to transfer the contents of a_1() and
a_2() to a_3 (x3, x4).
The first dimension of a_3(x3, 1) have to get the contents of a_1 und
the second dimension a_3(x4, 2) of the array a_2(x2).
Array sizes of a_1 and a_2 are same.
An example for my purpose:
a_1(45, 89, 12) a2(3, 14, 9) a3((45,
89, 12),(3, 14, 9) )
I tryed to apply this code but it comes to the error message regarding
wrong indexing:
For i = LBound(a_1) To i = UBound(a_1) for Array a_1
ReDim Preserve a_3(a3, 1)
a3 = a3 + 1
a_3(a3 - 1, 1) = a_1(x1)
Next
For i = LBound(a_2) To i = UBound(a_2) for Array a_2
ReDim Preserve a_4(a4, 2)
A4 = a4 + 1
a_4 (a4 - 1, 2) = a_2(x2)
Next
Probably you got an idea how to grapple it?
regards
LG
axtens - 16 Aug 2008 16:52 GMT
LG,
There seems to be a misunderstanding on how ReDim works. Below is code
that seems to do what you want, assuming that what you want is as
follows:
1. you have two 1D arrays of equal length
2. you want to "laminate" them, placing them side-by-side as two
columns in the one 2D array
So if you have a_1 as (1,2,3) and a_2 as (4, 5, 6) then you're wanting
a 2D array like this:
column column
0 1
row 0 1 4
row 1 2 5
row 2 3 6
This is the code. It was developed in VBScript and then tweaked in VB.
Dim a_1() As Variant ' as variant because i'm using Array() to fill it
a_1 = Array(1, 2, 3)
Dim a_2() As Variant ' as variant because i'm using Array() to fill it
a_2 = Array(4, 5, 6)
Dim a_3() As Integer
Debug.Print UBound(a_1), UBound(a_2)
ReDim a_3(UBound(a_1), 1) '", 1" because you have two columns, 0 and 1
' if a_1 and a_2 are not equal, redim with the maximum ubound of the
two
Debug.Print LBound(a_1), UBound(a_1)
For i = LBound(a_1) To UBound(a_1) ' for Array a_1
a_3(i, 0) = a_1(i)
Next
Debug.Print LBound(a_2), UBound(a_2)
For i = LBound(a_2) To UBound(a_2) ' for Array a_2
a_3(i, 1) = a_2(i)
Next
'show the contents of the first column
For i = 0 To 2
j = 0
Debug.Print i, j, a_3(i, j)
Next
'show the contents of the 2nd column
For i = 0 To 2
j = 1
Debug.Print i, j, a_3(i, j)
Next
I hope that helps.
Kind regards,
Bruce.