Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


splitting data from a file into an array

Posted on 2003-03-29
Medium Priority
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.

Question by:CodeHel
  • 2
  • 2
  • 2
  • +4

Expert Comment

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
End Sub
LVL 12

Accepted Solution

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

set ts = nothing
set fso = nothing
LVL 28

Expert Comment

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()
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
Close #ff
End Sub

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

LVL 22

Expert Comment

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


' 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)


Author Comment

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


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 :-)

LVL 22

Assisted Solution

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

Expert Comment

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.
LVL 12

Expert Comment

ID: 9301538
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.

Expert Comment

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.


Cleanup Volunteer

Expert Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

572 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