Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert 2D-Array to 1D-Array 1

Status
Not open for further replies.

wvandenberg

Technical User
Oct 24, 2002
125
CA
Hi,

I have a 2D array that I would like to convert to a one dimensional array. The 2D array (vChkArray(1,10)) is zero based. I would like the order of the one dimensional array to be (0,0),(1,0),(0,1),(1,1),(0,2),(1,2)...etc. I hope this makes sense but if it doesn't, please let me know and I'll try and explain it differently.

Thanks,
Wendy
 
When you say zero based array do you mean that the array starts at (0,0)?

Bob Scriver
 
I think this will get you what you want. I wasn't able to test this as I have to leave right now so give it a try and get back to me with the results.

dim i as integer
dim j as integer
Dim vChkArray(1,10) as integer
Dim vOneDim(20) as integer
i = 0
j = 0
k = 1
Do
vOneDim(k) = vChkArray(i,j)
i = IIF(i = 0, 1, 0)
j = IIF(i = 0, j + 1, j)
k = k + 1
Loop Until k = 21

Bob Scriver
 
Bob,

First off I would like to thank you for helping me. I tried out your code and it works and does what I requested. In response to your question, Yes, when I say zero based I mean that the first piece of data is in position (0,0). However,I need help with a few modifications if you are still interested.

Just for clarification, I will elaborate on what I am attempting to accomplish. I am importing text files into Excel and storing the filenames in the first row (row 0) of the 2d array. If the import has no problems, I store "True" in the same column as the filename in the 2D array but in the second row (row 1). If the import fails, I store "False". Then I would like to output two statements to my "Activity Log" that basically say:

"Files Site0, Site3, Site4, Site5, Site6, Site11 were imported sucessfully."

"Files Site1, Site2, Site7 were not imported."

With respect to the modification, the numbers I provided in my post are the positions of the data in the array and not the actual data in the array. The double array (VChkArray(1,10)) looks like this:

Site0 Site1 Site2 Site3 Site4 Site5 Site6 Site7 ... Site11
True False False True True True True False ... True

I would like the single array (vOneDim(10)) to look like this:

Site0True,Site1False,Site2False,Site3True ... Site11True

Once in the single arrray, I was thinking I would use something like Filter(vOneDim, "False") to extract all the "True" or "False" values.

So, in summary, what you wrote works but I would like to pass the double array to it and have it output the single array in the format listed above.

I hope I haven't totally confused you or scared you off. Thanks again for your attention and help.

Wendy
 
Give this a try. I hope I have interpreted what you want to do correctly.
dim i as integer
dim j as integer
Dim vChkArray(1,10) as integer
Dim vOneDim(20) as integer
i = 0
k = 1
Do
vOneDim(k) = vChkArray(0,i) & vChkArray(1,i)
i = i + 1
k = k + 1
Loop Until k = 11



Bob Scriver
 
Thank you Bob for your help. Your last suggestion worked just as I needed with a minor modification. I've included the final code below.

Function vConvertArray(vArray As Variant) As Variant
Dim i As Integer
Dim vOneDim(10) As Variant

i = 0
Do
vOneDim(i) = vArray(0, i) & vArray(1, i)
i = i + 1
Loop Until i = 11
vConvertArray = vOneDim
End Function

Thanks again and a star for you!

Wendy
 
Wendy: Hey great!! [thumbsup] Glad I could put that together for you. I wasn't sure if I understand exactly what you needed here. I figured that the Until condition needed to be changed.

Thanks for the star.

Bob Scriver
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top