?
Solved

splitting data from a file into an array

Posted on 2003-03-29
10
Medium Priority
?
222 Views
Last Modified: 2010-08-05
Hello, this is my first question here, so go easy on me :)

I am trying to produce a program in VB6 which grabs data from a file, and passes this data into an array.

The file is arranged as follows: BN27W38 FN10E11 NN13W18 et etc

I need to sort this data into a multi dimensional array of, say, StrLocation[][3], so it is in the following format within my program:

B   N27   W38
F   N10   E11
N   N13   W18

Can anyone help me with the code to do this? The format of the file can be changed if it makes it easier at all.

Thanks!!
0
Comment
Question by:CodeHel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +4
10 Comments
 
LVL 3

Expert Comment

by:emadat
ID: 8230923
From Your "Project", "References" menu item, add "Microsoft Scripting Runtime" to your project and modify the followig function to do the job for you.

Basically, what you need to do is to consider the file as a dual-dimntional array, with the number of ines represent one dimension and the number of entries in each line represents the other dimension.

We will read the first line, convert it into a one dimentional array using the Split function; then we will use the number of entries from this array as -say the x dimention- of our dual-dimntional array.

Then we will continue reading each line and "ReDim" as we go.

That's it..!
The format is all yours provided that you do the following:
- Keep number of entries in each line to bes the same -3 in your example-
- Keep the delimeter between fields to be consistent in all your fields.

Here is the function:
 
Private Sub Command1_Click()
Dim fso As FileSystemObject
Dim f As TextStream, S$
Dim sArray, tmpArray
Dim U%, L%, X%, Y%
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.OpenTextFile("c:\1\1.txt", ForReading)
    ' Read the first line to set the first dimention of our array
    S = f.ReadLine
    tmpArray = Split(S, " ")
    Y = 0
    U = UBound(tmpArray)
    L = LBound(tmpArray)
    ReDim sArray(U - L + 1, Y)
    For X = L To U
        sArray(X, Y) = tmpArray(X)
    Next X
    While Not f.AtEndOfStream
        Y = Y + 1
        ReDim Preserve sArray(U - L + 1, Y)
        S = f.ReadLine
        tmpArray = Split(S, " ")
        For X = L To U
            sArray(X, Y) = tmpArray(X)
        Next X
    Wend
End Sub
0
 
LVL 12

Accepted Solution

by:
jyokum earned 152 total points
ID: 8230994
This example will only handle 100 elements in the first dimension of the array so you'll probably have to modify it a bit to handle X elements. You can use redim to change the upper bound. After it runs, arData will be a 2 dimensional array with the data from your file.

Add a reference to "Microsoft Scripting Runtime"

dim fso as FileSystemObject
dim ts as TextStream
dim strData as String
dim arData(100) as Variant
dim i as integer

Set fso = New FileSystemObject
Set ts = fso.OpenTextFile("c:\filename.txt", ForReading, False, TristateFalse)

do until ts.AtEndOfStream
    strData = ts.ReadLine
    arData(i) = Split(strData," ") 'this will create an array of the elements on one line
    i = i + 1
loop

set ts = nothing
set fso = nothing
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 8231006
I would do it without using FSO.Why add the overhead of FSO for something as simple as reading a textfile.

this will add the items to a listbox:

Private Sub Command1_Click()
List1.Clear
Dim sFile As String, Ln As String
Dim arrLn() As String
Dim ff As Integer, i As Integer
sFile = "C:\Somefile.txt"
ff = FreeFile
' open file
Open sFile For Input As #ff
' read file until eof
Do Until EOF(ff)
Line Input #ff, Ln
' split line in file by spaces into array
arrLn = Split(Ln, " ")
' loop through array
For i = 0 To UBound(arrLn)
' breakdown array element and add to listbox
List1.AddItem Left(arrLn(i), 1) & vbTab & Mid(arrLn(i), 2, 3) & vbTab & Mid(arrLn(i), 5)
Next i
Loop
Close #ff
End Sub
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 22

Expert Comment

by:rspahitz
ID: 8232981
Very good, VinnyD.  The only thing I'd add is that CodeHel wants the info into a two-dimensional array rather than a listbox so:

' add these near the top
dim strResults() as string
dim iLineCntr as integer

iLineCntr=0

'...
' put this in place of the list1.additem:
Redim strResults(2,iLineCntr)
strResults(0,iLineCntr) = Left(arrLn(i), 1)
strResults(1,iLineCntr) = Mid(arrLn(i), 2, 3)
strResults(2,iLineCntr) = Mid(arrLn(i), 5)
iLineCntr=iLineCntr+1
'...

0
 

Author Comment

by:CodeHel
ID: 8234984
thankyou! This is helping a great deal. I am taking the non-object answer as my answer (i don't fully understand objects yet...). But i would like to ask a further question (not sure if i should start another topic, but i will try here first..).

Now that i have the array, as follows:

B   N27   W38
F   N10   E11
N   S13   W18

etc

I now need to:

For each "row" in the array
    Do calculation on the number part of Column 2
           IF N assign +ive sign to the answer
           IF S assign -ive sign to the answer
    Do calculation on the number part of column 3
           IF E assign +ive sign to the answer
           IF W assign -ive sign to the answer

Draw Point (Answer2, Answer1)
     IF column 1 = B  draw in red
     IF column 1 = F  draw in green
     IF column 1 = N  draw in blue


Your answers have already helped me a lot, thanks for that. I've been trying to do this for days :-)

0
 
LVL 22

Assisted Solution

by:rspahitz
rspahitz earned 148 total points
ID: 8239837
iItemCount = UBound(strResults,2) ' find the number of items in the second dimension

For iLineCntr = 1 to iItemCount
  ' Convert N to + and S to - in strResults(1,x)
  ' Convert E to + and W to - in strResults(2,x)
  'Several ways to handle this such as
  select case left$(strResults(1,iLineCntr), 1)
    case "N" ', "n" maybe also allow lower case?
      intDegrees = val(mid$(strResults(1,iLineCntr), 2))
    case "S"' , "s"
      intDegrees = - val(mid$(strResults(1,iLineCntr), 2))
    case else
       debug.print "Invalid code found in column 2"
       intDegrees = 0
  end select
  intVertical = intDegrees

  select case left$(strResults(2,iLineCntr), 1)
    case "E" ', "e"
      intDegrees = val(mid$(strResults(2,iLineCntr), 2))
    case "W"' , "w"
      intDegrees = - val(mid$(strResults(2,iLineCntr), 2))
    case else
       debug.print "Invalid code found in column 3"
       intDegrees = 0
  end select
  intHorizontal = intDegrees

  select case strResults(0,iLineCntr)
    case "B"
       lColor = vbRed
    case "F"
       lColor = vbGreen
    case "N"
       lColor = vbBlue
    case else
       lColor = vbBlack
  end select

  ' Draw point onto form (may need to draw into picturebox)
  Me.PSet (intHorizontal, intVertical), lColor
next iLineCntr
0
 

Expert Comment

by:CleanupPing
ID: 8531359
Hi CodeHel,
This old question (QID 20567458) needs to be finalized -- accept an answer, split points, or get a refund.  Please see http://www.cityofangels.com/Experts/Closing.htm for information and options.
0
 
LVL 12

Expert Comment

by:jyokum
ID: 9301538
CodeHel,
This has been open 161 days and there hasn't been a comment added in 114 days.
Please select a comment as the solution or give us an update.
0
 
LVL 6

Expert Comment

by:GPrentice00
ID: 9440711
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

 -->Split between jyokum and rspahitz

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER

GPrentice00
Cleanup Volunteer
0
 
LVL 6

Expert Comment

by:GPrentice00
ID: 9440712
thankyou! This is helping a great deal. I am taking the non-object answer as my answer (i don't fully understand objects yet...). But i would like
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question