|
-
Apr 13th, 2002, 01:49 AM
#1
How do I read an array from a text file?
Hi,
I am new to VB and it took me a whole half hour to figure out that you don't append every line with a semicolon!!
I have a 71 by 71 integer matrix that is tab delimited stored in a text file. I want to read this into a 71 by 71 integer array. Could some please tell me how to go about it?
I tried using a Variant, and the Split method and casting the result by Int(), but got Type Mismatches over and over again.
Thank you in advance.
Anuj
-
Apr 13th, 2002, 02:24 AM
#2
-= B u g S l a y e r =-
hmm... arrays, yet another thing I don't do well...
I'll stick my neck out anyway.. (BeachBum will be all over me after this )
VB Code:
Option Explicit
Private Const NumRows = 70
Private Const NumCols = 70
Private Sub Command1_Click()
Dim sArr() As String
Dim sTmp() As String
Dim i As Integer
Dim x As Integer
Dim iArr(NumRows, NumCols) As String
Dim sFileName As String
sFileName = "C:\matrix.txt"
Open sFileName For Input As #1
sArr = Split(Input(LOF(1), 1), vbCrLf)
Close #1
For i = 0 To UBound(sArr())
sTmp = Split(sArr(i), vbTab)
For x = 0 To NumCols
iArr(i, x) = Int(sTmp(x))
Next x
Next i
End Sub
does what u want
-
Apr 13th, 2002, 02:45 AM
#3
Frenzied Member
Hmm peet, you forgot to explain to him about the concept of
delimiters! ^^
If you don't already know, a delimiter is like something in between
2 substrings. For instance in this case, peet used a tab as
the delimiter. So for instance the delimiter would be used like this
"ArrayEntry0 ArrayEntry1"
Then you could literally take whats in this string and "split" it into
an array.
VB Code:
Dim MyArray() As String
MyArray = Split("ArrayEntry0 ArrayEntry1", vbTab) 'This splits them by the delimiter
MsgBox MyArray(0) 'Notice how this returns "ArrayEntry0"
MsgBox MyArray(1) 'Notice how this returns "ArrayEntry1"
Just making sure the dude understands what you were talking about peet. 
Hope this helps, laterz.
-
Apr 13th, 2002, 05:03 AM
#4
-= B u g S l a y e r =-
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
|