Solved

parse file

Posted on 2004-11-02
301 Views
Last Modified: 2010-05-02
I have a file that looks like ISA*00**00*27*00523*ZZ*M4788
Could you give me example code on how to parse this file that has * seperating elements?





0
Question by:lindapat
    5 Comments
     
    LVL 28

    Expert Comment

    by:vinnyd79
    Private Sub Command1_Click()
    Dim ff As Integer, Ln As String, arrLn() As String
    ff = FreeFile
    Open "C:\Myfile.txt" For Input As #ff
    Do Until EOF(ff)
    Line Input #ff, Ln
    ' split line into array by "*"
    arrLn = Split(Ln, "*")

    ' loop through array
    Dim i As Integer
    For i = 0 To UBound(arrLn)
        If arrLn(i) <> "" Then MsgBox arrLn(i)
    Next i

    DoEvents
    Loop
    Close #ff

    End Sub
    0
     
    LVL 4

    Expert Comment

    by:learning_t0_pr0gram
    TheFile = your file

    '==code==
    Dim tmp() as string

    tmp() = split(TheFile, "*")

    For a = 0 to ubound(TheFile)
       Debug.Print tmp(a)
    Next
    0
     

    Author Comment

    by:lindapat
    I am new at VB.net so please excuse me.  Will this code work? If not would you correct it for me? I want to show each element in label I have created on a form.
    Private Sub read835()
            Dim isaLine As String
            Dim elementSep, compositeSep, segmentSep As String
            Dim x As Integer
            Dim tmp() As String
            isaLine = "ISA*00*          *00*          *27*00523          *ZZ*M4788          *040414*2351*U*00401*410500028*0*P*:~"
            elementSep = isaLine.Substring(103, 1)

            For x = 0 To isaLine.Length
                tmp = Split(isaLine, "*")

            Next
            lblMsg.Text = tmp(x)
    0
     
    LVL 1

    Accepted Solution

    by:
    For x = 0 To isaLine.Length
                tmp = Split(isaLine, "*")
    Next
    ^
    This is wrong because the split function acts like a loop itself and parses the string.  All you would need is

        tmp() = Split(isaLine, "*")
           
    0
     
    LVL 4

    Expert Comment

    by:learning_t0_pr0gram
    yes but then there will be blank lines.. for instance..

    if you split it by * it will look like this:

    tmp(0) = "ISA"
    tmp(1) = "00"
    tmp(2) = "          "
    tmp(3) = "00"
    tmp(4) = "          "
    tmp(5) = "27"
    tmp(6) = "00523"
    tmp(7) = "ZZ"

    etc...

    so which lines do you want to get out??  if you do it like i had it, u could get all the information out and check if it's all spaces, and if it is skip it.. such as:


    Dim tmp() as string

    tmp() = split(TheFile, "*")

    For a = 0 to ubound(TheFile)
       if Trim(tmp(a)) <> "" Then MsgBox tmp(a)
    Next

    ubound fwould be the uppermost index in the array, and trim replaces spaces on the ends of the string, so trim("       ") would be the same as ""
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    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…
    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    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…

    934 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now