newbie quesion: how to do this

zhujp98
09-06-2003, 10:43 AM
I have a test file



//data.txt
ACD this is an animal 234
BDF best practice 678
AEF boo foo cool 890




I want to read this file to three Arrays
Array1 contains three strings ACD BDF AEF
array2 contains three strings " this is an animal" "best practice"
" boo foo cool"
array3 contains three string 234 678 890

The trick here is array2 will contain strings seperate by " ", i get stucked

Any help in how i can i do that will be appreciated.
thank

zak2zak
09-06-2003, 10:54 AM
May be this helps...
I supposed U should use Dynamic Arrays

'Declaration Top Module Or Form...
Dim sString() As String
---------------------------------------
Private Sub OpenFile(FilePathName As String)
Dim sAppend() As String
Dim sSplit() As String
Dim fn As Long
Dim counter As Integer
fn = FreeFile
Open FilePathNameFor Input As #fn
counter = 0
Do Until EOF(fn)
ReDim Preserve sString(counter) As String
Line Input #fn, sAppend(counter)
counter = counter + 1
Loop
Close #fn
Redim sString(UBound(sAppend), 2) As String
For counter = LBound(SsAppend) To UBound(SsAppend)
sSplit = Split(sAppend(counter)," ", , vbTextCompare)
sString(counter,0) = sSplit(0)
sString(counter,1) = sSplit(1)
sString(counter,2) = sSplit(2)
Erase sSplit
Next counter
Erase sAppend
End Sub

passel
09-06-2003, 11:04 AM
If you're using VB6 and you are sure that all the lines are going to be
formatted like this, and you only need the three fields, you can use the
InStrRev, to search from the end of the string, and InStr to search from
the beginning, i.e.

Dim a(3), s As String
Dim b As Integer, e As Integer

s = "ACD this is an animal 234"

b = InStr(s, " ") 'find the first space
e = InStrRev(s, " ") 'find the last space
a(1) = Left$(s, b - 1) 'first field
a(2) = Mid$(s, b + 1, e - b - 1) 'second field
a(3) = Mid$(s, e + 1) 'third field

For e = 1 To 3
Debug.Print a(e)
Next

Lar_19
09-06-2003, 12:25 PM
Or you could Split() each of the input lines, assign the first element of the resulting array to Array1, concatenate all but the first and last elements and assign that to Array2, and assign the last element to Array3. As you can see, there are several approaches you can take. :)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum