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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert array to multi-dimensional array

Status
Not open for further replies.

ciberperson

Programmer
Aug 31, 2000
29
CA
I have an array which was created by reading in lines from my FileSystemObject. Each line has 8 fields and I want to create a 2 dimensional array from the original array so that I can select information based on a column.

Here's what I have:

While not theFile.AtEndOfStream
i=i+1
arrLines(i) = theFile.ReadLine
Wend
Redim Preserve arrLines(i)

and I want to take arrLines and make it arrFile(i,m) where m is the 8 columns . Can someone help me with this logic? Thanks in advance.
 
Try something like this:

Dim arrFile(0,8)
Dim i
i = 0
While not theFile.AtEndOfStream
i=i+1
arrLines(i,0) = theFile.ReadLine
Wend
Redim Preserve arrLines(i,8)
 
I've tried your example but I think you meant arrFile(0,8) to be arrLines(0,8) and that looks like it would work, but I get a "Subscript out of range: '1'" error on the line arrLines(i,0)=theFile.ReadLine

I'm assuming that in VBscript the multi-dimension array is reading arrLines(rows)(columns) - am I correct?

Thanks for your patience!
 
If you read up on arrays just a little you would find the reason for the error. (ps. that is probably the most common error with arrays)

Try This:

Dim arrLines(0,7)
Dim i
i = 0
While not theFile.AtEndOfStream
Redim Preserve arrLines(i,7)
arrLines(i,0) = theFile.ReadLine
i=i+1
Wend


Kris
- I don't mind going nowhere as long as it's an interesting path.
 
Actually I've read up on arrays more "than just a little" and first you have locked the array with Dim arrLines(0,7) but that's OK cause I just changed it to redim.

Also if the object theFile was a very long text file then the Redim Preserve within the loop will take a serious toll on performance but my file should never exceed 400 lines so I think that will be OK.

And I do know that the subscript begins at 0 not 1, but I'm still getting a subscript out of range error. I've even changed i=-1 so that when it incremented it would be at 0 but that did not change the error message.

I was also under the impression that redimming a multi-dimensional array only adjusted the last dimension - is that correct?

I appreciate your help on this and I look forward to any other ideas which might help.
 
Maybe this is just too obvious but if Dim ArrLines(0,7) then you cant increment on the 0 change the I and the 0 around
Dim arrLines(0,7)
Dim i
i = 0
While not theFile.AtEndOfStream
arrLines(0,i) = theFile.ReadLine
i=i+1
Wend Saturday 12.00
im6.gif
im2.gif
im2.gif
20.00
im5.gif
im4.gif
im7.gif
3.00am
im8.gif
Sunday [img http
 
Just for posterity here's the solution that is working for me:
i=0
dim arrFile(1000,7)

While not theFile.AtEndOfStream
i = i + 1
t = theFile.ReadLine
s = Split(t,Chr(9))
arrFile(i,0)= s(0) 'kit
arrFile(i,1)= s(1) 'version
arrFile(i,2)= s(2) 'state
arrFile(i,3)= s(3) 'date
arrFile(i,4)= s(4) 'time
arrFile(i,5)= s(5) 'qty
arrFile(i,6)= s(6) 'component
arrfile(i,7)= s(7) 'tag
Wend
Redim Preserve arrLines(i,7)

This creates my 2 dimensional array and lets me access each column if needed.

Garyc123, I did not try your suggestion to switch the i and 0.. I have never seen that mentioned in any of my reading on arrays. Maybe one day I'll get a chance to give it a try.
###
 
Was just making a comment that in all the examples above you were incrementing the i on the zero part instead of the 7 part i.e Dim arrLines(0,7) but then in the rest of the code you were doing arrLines(i,7) which would give you the error you were getting. It should be arrLines(0,i)

Saturday 12.00
im6.gif
im2.gif
im2.gif
20.00
im5.gif
im4.gif
im7.gif
3.00am
im8.gif
Sunday [img http
 
You are right Gary and the code that ciber just posted is also correct.

I just wrote the code from memory and you see where that got me? [thumbsdown]

I was shooting for ciber to pickup on the concept (which s/he did) and not the literal translation of the code.

Kris
[smilejap] If you have nothing to lose, you can try everything. - Yiddish (on business)
 
Chris and Gary - thanks for your help - I enjoy Tek-tips forum most of all the places I go for help.

And I'm a "she"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top