|
-
May 8th, 2002, 04:34 PM
#1
Two Dimentional Array and Split? Stumped.
I have tried to find an already posted answer to this, but I could not so here goes...
I have a text file that looks like this:
header info line1
header info line 2
header info line 3
9.880662e-001 1.815209e-001 -1.414392e+000 -5.080986e-001 1.729634e-001 -1.791693e-001
9.880662e-001 1.815209e-001 -1.414392e+000 -5.080986e-001 1.729634e-001 -1.791693e-001 -9.064005e-002
9.880662e-001 1.815209e-001 -1.414392e+000 -5.080986e-001 1.729634e-001 -1.791693e-001 -9.064005e-002
9.880662e-001 1.815209e-001 -1.414392e+000 -5.080986e-001 1.729634e-001 -1.791693e-001 -9.064005e-002
9.880662e-001 1.815209e-001 -1.414392e+000 -5.080986e-001 1.729634e-001 -1.791693e-001 -9.064005e-002
9.880662e-001 1.815209e-001 -1.414392e+000 -5.080986e-001 1.729634e-001 -1.791693e-001 -9.064005e-002
I have truncated the real length of the text file to fit in the message board, but I do know how many values are across and how many lines down.
What I want to do is stuff these values into a two dimentional array.
I have this code so far:
Dim myArray() as Double
Dim i as Integer
Open CommonDialog1.FileName For Input As #1
Dim lineNum As Integer
Do Until EOF(1)
Line Input #1, sLine
lineNum = lineNum + 1
If lineNum > 3 Then
For i = 1 To valuesAcross
myArray(lineNum - 3, 1) = Split(CDbl(sLine), " ")
Next i
End If
Loop
Close #1
Its the poulating of the array that has me stumped. Can you please help. I am new to arrays. I understand how they work, but I am new to actually using them
Thank you.
-
May 8th, 2002, 04:37 PM
#2
Stuck in the 80s
I don't think this is possible with two dimensional arrays.
-
May 8th, 2002, 04:39 PM
#3
Stuck in the 80s
You could do this:
VB Code:
Dim lns() As String, tmp() As String
tmp = Split("adf adsf adf adsf adsf adsf", " ")
ReDim lns(1, UBound(tmp))
For i = 0 To UBound(tmp)
lns(1, i) = tmp(i)
Next
But you've stated that the file is large, so that might not be a good idea with the loop. I believe there's an API function to do this, CopyMemory, maybe?
-
May 8th, 2002, 04:40 PM
#4
The UBound command will give the count +1 of the number of elements in the array.
For example.. to iterate through the array you would do something like this:
VB Code:
Dim iCount As Integer
Dim iCurr As Integer
Dim arrSomeArray() As String
arrSomeArray = Split("1 2 3 4 5", " ")
iCount = UBound(arrSomeArray)
For iCurr = 0 To iCount
Debug.Print arrSomeArray(iCurr)
Next iCurr
-
May 8th, 2002, 04:43 PM
#5
Stuck in the 80s
Originally posted by joan_fl
The UBound command will give the count +1 of the number of elements in the array.
For example.. to iterate through the array you would do something like this:
That's not the kind of array he wanted. And the Ubound will give the count - 1 of items in the array. Ubound will give the upperbound. As in arrTemp(0 to 19) will give 19.
-
May 8th, 2002, 04:47 PM
#6
Stuck in the 80s
fntzlnd: Also, you must declare (Dim) the size of the array, otherwise you'll get subscript out of range errors.
Ex:
VB Code:
Dim arr() As Integer
arr(0) = 1 'this is bad
'do this
Redim arr(0)
arr(0) = 1
'if the array already has data, do
Redim Preserve arr(1)
arr(1) = 2
However, for two dimensional arrays, there's different rules to follow when Redimming.
-
May 8th, 2002, 05:27 PM
#7
THANKS
Thanks Hobo,
That idea helped alot.
-
May 8th, 2002, 05:37 PM
#8
Dim MyArray() as variant
Loop with a loopvariable and read one line at a time:
MyArray(LoopVar) = ReadNextLine()
In ReadNextLine, create an array and read all five across elements in to it then, assign the 5 dimensional array to readline before the function ends.
Then, each MyArray will contain an array of the items across.
I'll give you code in a minute.
Need to re-register ASP.NET?
C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i
(Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)
-
May 8th, 2002, 05:39 PM
#9
Stuck in the 80s
Originally posted by Lord_Rat
Dim MyArray() as variant
Loop with a loopvariable and read one line at a time:
MyArray(LoopVar) = ReadNextLine()
In ReadNextLine, create an array and read all five across elements in to it then, assign the 5 dimensional array to readline before the function ends.
Then, each MyArray will contain an array of the items across.
I'll give you code in a minute.
You're either really smart, or don't know what you're talking about. Because all of that just flew right by me.
-
May 8th, 2002, 05:44 PM
#10
VB Code:
Option Explicit
Dim MyArray() As Variant
Dim NumRows As Long
Dim NumCols As Long
Private Sub Form_Load()
'arbitrary
NumRows = 10
NumCols = 5
Dim loopvar As Long
loopvar = 0
ReDim MyArray(NumRows)
For loopvar = 0 To NumRows
MyArray(loopvar) = ReadNextLine
Next
'next line is number of rows down by what column over (which seems backward, I know)
MsgBox MyArray(2)(3)
End Sub
Private Function ReadNextLine()
Dim TempArray() As String
ReDim TempArray(NumCols)
Dim loopvar As Long
'if using a file for input, the next section would be
'replaced with a read of the elements in the file
For loopvar = 0 To NumCols
TempArray(loopvar) = Chr(64 + loopvar)
Next
ReadNextLine = TempArray
End Function
Need to re-register ASP.NET?
C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i
(Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)
-
May 8th, 2002, 06:05 PM
#11
Do you understand the code?
Need to re-register ASP.NET?
C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i
(Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)
-
May 8th, 2002, 07:09 PM
#12
Stuck in the 80s
-
May 8th, 2002, 07:15 PM
#13
A variant data type is capable of storing almost any data type available to Visual Basic.
This includes arrays.
Thus, you can create an array of variants and store in each array element another array.
Get ready for the next part.
Each element's array does not have to match the same array dimensions of the other elements.
Thus, if
MyArray(1) had an array of 5 elements,
MyArray(2) could have an array of 6 elements.
It makes no difference.
My recommendation is to put a break point on the last line in the form load code above.
When you reach that line, add my array as a watch item.
You will see that when you expand it, you get a list of the elements of the MyArray, but then each element can be further expanded to show another array at a different level.
In order to access array elements built this way, you access them with sets of parentheses
(toplevel)(nextlevel)...(andSoOn)...(LastEmbeddedArray)
The LastEmbeddedArray can be of any type, but all arrays above it must be variants.
Need to re-register ASP.NET?
C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i
(Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)
-
May 8th, 2002, 07:17 PM
#14
Stuck in the 80s
I just get a headache trying to think about it.
-
May 8th, 2002, 07:19 PM
#15
Lol
Rarely would you need a data type of that design, but this is a case where it would make your code much simpler.
If it causes a migraine, then forget about it and pretend this never happened.
Need to re-register ASP.NET?
C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i
(Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|