Mombu the Microsoft Forum sponsored links

Go Back   Mombu the Microsoft Forum > Microsoft > Converting a 1-Dimensional array to a 2- Dimensional array
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 25th January 15:54
pejo
External User
 
Posts: 1
Default Converting a 1-Dimensional array to a 2- Dimensional array



Val_0_0,Val_0_1,Value_0_2,Value_0_3
Val_1_0,Val_1_1,Value_1_2,Value_1_3
Val_2_0,Val_2_1,Value_2_2,Value_2_3
Val_3_0,Val_3_1,Value_3_2,Value_3_3
Val_4_0,Val_4_1,Value_4_2,Value_4_3
Val_5_0,Val_5_1,Value_5_2,Value_5_3
Val_6_0,Val_6_1,Value_6_2,Value_6_3


txt field as above...

I read into a one dimensional array using:

Dim objFSO, objTS
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

Set objTS = objFSO.OpenTextFile("a:\value_list.txt")

''Read into a string
Dim s
s = objTS.ReadAll

''Create One Dimensional Array by splitting them at line feed/carriage
return character
Dim one_d_array
one_d_array = split(s, vbCrLf)

'''I have a one dimensional array if I was to loop through and line breaks
it would print to the screen as above.

For i = 0 to ubound(one_d_array) ''scroll through array one by one

response.Write(one_d_array(i) & "<BR>")

Next


What I want to do here is actually turn it into a two dimensional array

And split it on the commas ''Obviously the below does not work.

For i = 0 to ubound(one_d_array)
'two d_array(i) = aFile 'split(aFile,",")
next

It's easy for me to take a two or three dimensional array and make it one
dimensional but for some reason
I'm drawing blanks trying to do it the other way around.

Thanks in advance for any help.


Pejo.
  Reply With Quote


  sponsored links


2 25th January 15:54
bob barrows
External User
 
Posts: 1
Default Converting a 1-Dimensional array to a 2- Dimensional array



Almost. You have two choices.
You can create an array of arrays (recommended):
ar=split(s,vbcrlf)
for i = 0 to ubound(ar)
ar(i) = split(ar(i),",")
next
for i = 0 to ubound(ar)
for j = 0 to ubound(ar(i))
Response.Write ar(i)(j) & "<BR>"
next
next

Or create a separate multidimensional array, populating it from the first
array:

The reason I recommend the array of arrays is that you may not be sure how
many values will be on each line: there could be a varying number of values
on each liine, or the liines could all contain the same number of values,
but it could vary from one text file to the next. The array of arrays will
handle either situation.

--
HTH,
Bob Barrows - ASP MVP
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
  Reply With Quote
3 25th January 15:54
pejo
External User
 
Posts: 1
Default Converting a 1-Dimensional array to a 2- Dimensional array


Thanks for your reply Bob...
The number of values will always remain constant in the text file so I think
it would be
simpler to create the multi-dimensional array from the one dimensional one.
But that is where I'm having trouble.
I can't seem to figure out how to do it.

I know it should be simple but I can't seem to get it right.
  Reply With Quote
4 25th January 15:54
bob barrows
External User
 
Posts: 1
Default Converting a 1-Dimensional array to a 2- Dimensional array


Try this (untested):

dim ar, ar2d(), i, ar2, j
ar=split(s,vbcrlf)
redim ar2D(3,ubound(ar))
for i = 0 to ubound(ar)
ar2=split(ar(i),",")
for j = 0 to 3
ar2d(j,i) = ar2(j)
next
next

Bob Barrows
  Reply With Quote
5 25th January 15:55
kyle m. burns
External User
 
Posts: 1
Default Converting a 1-Dimensional array to a 2- Dimensional array


Another option would be to open the file as a recordset using ADO.
  Reply With Quote
6 25th January 15:56
ivan panasiuk
External User
 
Posts: 1
Default Converting a 1-Dimensional array to a 2- Dimensional array


Here is one way...

Dim objFSO, objTS
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTS = objFSO.OpenTextFile("value_list.txt")

'Read into a string
Dim s
s = objTS.ReadAll

'Create One Dimensional Array by splitting them at line feed/carriage

Dim one_d_array, temp_array
one_d_array = split(s, vbCrLf)

Dim twod_array(100,100)

For i = 0 to UBound(one_d_array) 'scroll through array one by one
temp_array = split(one_d_array(i),",")
For j = 0 to UBound(temp_array)
twod_array(i,j)=temp_array(j)
Wscript.Echo twod_array(i,j)
Next
Next
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666