Results 1 to 8 of 8

Thread: Parsing Experts!!! Need help.

  1. #1

    Thread Starter
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Parsing Experts!!! Need help.

    Hello guys! Im having trouble parsing this file although it's a bit easy i can't get it neat and fast. Any suggestion is well appreciated.

    Here is my sample text file.

    Name <1:WALKER, JOHNNY>
    File Number <2:123456>
    Record Number <3:111111>
    Filename: WALKER, JOHNNY_123456_111111 script.txt
    <End>

    CASE: 0001
    NAME: JOHNNY WALKER
    RECORD NUMBER: 111111
    Description is here. The quick brown fox jumps over the lazy dog. Blah blah blah.

    CASE: 0002
    NAME: JACKIE CHAN
    RECORD NUMBER: 222222
    Pneumonoultramicroscopicsilicovolcanoconiosis.

    CASE: 0003
    NAME: JET LI
    RECORD NUMBER: 333333
    Lol lol lol lol lol lol lol.

    CASE: 0004
    NAME: WONG FEI HONG
    RECORD NUMBER: 444444
    Any random description here.
    I need to separate this file into 4 textfiles however the header must be changed according to the name, record number and description. Don't bother the case number and the Filenumber is the same for all of them.

    Case 001 have already the correct header. So it will be saved as "WALKER, JOHNNY_123456_111111 script.txt"

    Name <1:WALKER, JOHNNY>
    File Number <2:123456>
    Record Number <3:111111>
    Filename: WALKER, JOHNNY_123456_111111 script.txt
    <End>

    CASE: 0001
    NAME: JOHNNY WALKER
    RECORD NUMBER: 111111
    Description is here. The quick brown fox jumps over the lazy dog. Blah blah blah.
    Case 002 shoud be saved as "CHAN, JACKIE_123456_222222 script.txt" and the header should be replaced accordingly.

    Name <1:CHAN, JACKIE>
    File Number <2:123456>
    Record Number <3:222222>
    Filename: CHAN, JACKIE_123456_222222 script.txt
    <End>

    CASE: 0002
    NAME: JACKIE CHAN
    RECORD NUMBER: 222222
    Pneumonoultramicroscopicsilicovolcanoconiosis.
    and so on and so forth. the cases could reached up to 30 cases that's why im looking for an elegant way of doing this or even faster.

    So there's one file and the output would be 4 files in that case.

    Thank you and God bless!
    Attached Files Attached Files
    Last edited by zynder; Apr 11th, 2007 at 05:07 AM. Reason: Attached sample file.

  2. #2
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Parsing Experts!!! Need help.

    Not sure if this is what your after, but thought i'd give it a try


    It will set up an array with the data to be written into the log. Use GetData to return the log contents you need.
    Code:
    Private Type CaseData
        CaseNumber As Long
        NAME As String
        RecordNumber As Long
        Description As String
    End Type
    Dim Cases() As CaseData
    
    Private Sub Command1_Click()
    Dim Prts() As String
    Dim x As Integer
    Dim Num As Integer
    Dim Pos As Integer
    
    Prts() = Split(Text1.Text, vbCrLf)
        
        For x = 0 To UBound(Prts())
        
            If InStr(1, Prts(x), "CASE:", vbTextCompare) And Pos = 0 Then
                ReDim Preserve Cases(Num)
                Cases(Num).CaseNumber = Val(Mid(Prts(x), 6))
                Pos = 1
            End If
            If InStr(1, Prts(x), "NAME:", vbTextCompare) And Pos = 1 Then
                Cases(Num).NAME = Mid(Prts(x), 6)
                Pos = 2
            End If
            If Pos = 3 Then
                Cases(Num).Description = Prts(x)
                Num = Num + 1
                Pos = 0
            End If
            If InStr(1, Prts(x), "RECORD NUMBER:", vbTextCompare) And Pos = 2 Then
                Cases(Num).RecordNumber = Val(Mid(Prts(x), 15))
                Pos = 3
            End If
            
        Next
    End Sub
    
    Private Function GetData(Index As Integer) As String
    Dim Nms As String
    
    Nms = Split(Trim(Cases(Index).NAME), " ")
    GetData = "Name " & vbTab & "<1:" & Nms(1) & ", " & Nms(0) & ">" & vbCrLf & _
                 "File Number " & vbTab & "<2:123456>" & vbCrLf & _
                 "Record Number " & vbTab & "<3:" & Cases(Index).RecordNumber & ">" & vbCrLf & _
                 "Filename: " & Nms(1) & ", " & Nms(0) & "_123456_" & Cases(Index).RecordNumber & " script.txt" & vbCrLf & _
                 "<End>" & vbCrLf & vbCrLf & _
                 "CASE: " & Format(Cases(Index).CaseNumber, "0000") & vbCrLf & _
                 "NAME: " & Cases(Index).NAME & vbCrLf & _
                 "RECORD NUMBER: " & Cases(Index).RecordNumber & vbCrLf & _
                 Cases(Index).Description
    End Function

  3. #3

    Thread Starter
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Parsing Experts!!! Need help.

    Thanks for your reply however I need to open the file then create another 4 textfiles in that case. something like this.

    vb Code:
    1. 'path
    2. myPath = "C:\test\sample.txt"
    3.  
    4. ff = FreeFile
    5.  
    6. Open myPath For Input As ff
    7.  
    8. myArr() = Split(Input(LOF(ff), ff), vbCrLf & vbCrLf)
    9.  
    10. Close ff

    then

    vb Code:
    1. Open "theCorrectFilenameHere" For Output As ff

    Thanks again for the help!


    Hey Andrew I was wondering where is the log written.

  4. #4
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Parsing Experts!!! Need help.

    Added the part to write the files. The changed code (from my above post) is in bold.
    Code:
    Private Type CaseData
        CaseNumber As Long
        NAME As String
        RecordNumber As Long
        Description As String
    End Type
    Dim Cases() As CaseData
    
    Private Sub Command1_Click()
    Dim Prts() As String
    Dim x As Integer
    Dim Num As Integer
    Dim Pos As Integer
    
    Prts() = Split(Text1.Text, vbCrLf)
        
        For x = 0 To UBound(Prts())
        
            If InStr(1, Prts(x), "CASE:", vbTextCompare) And Pos = 0 Then
                ReDim Preserve Cases(Num)
                Cases(Num).CaseNumber = Val(Mid(Prts(x), 6))
                Pos = 1
            End If
            If InStr(1, Prts(x), "NAME:", vbTextCompare) And Pos = 1 Then
                Cases(Num).NAME = Mid(Prts(x), 6)
                Pos = 2
            End If
            If Pos = 3 Then
                Cases(Num).Description = Prts(x)
                Num = Num + 1
                Pos = 0
            End If
            If InStr(1, Prts(x), "RECORD NUMBER:", vbTextCompare) And Pos = 2 Then
                Cases(Num).RecordNumber = Val(Mid(Prts(x), 15))
                Pos = 3
            End If
            
        Next
    End Sub
    
    Private Function GetData(Index As Integer) As String
    Dim Nms As String
    
    Nms = Split(Trim(Cases(Index).NAME), " ")
    GetData = "Name " & vbTab & "<1:" & Nms(1) & ", " & Nms(0) & ">" & vbCrLf & _
                 "File Number " & vbTab & "<2:123456>" & vbCrLf & _
                 "Record Number " & vbTab & "<3:" & Cases(Index).RecordNumber & ">" & vbCrLf & _
                 "Filename: " & GetFileName & vbCrLf & _
                 "<End>" & vbCrLf & vbCrLf & _
                 "CASE: " & Format(Cases(Index).CaseNumber, "0000") & vbCrLf & _
                 "NAME: " & Cases(Index).NAME & vbCrLf & _
                 "RECORD NUMBER: " & Cases(Index).RecordNumber & vbCrLf & _
                 Cases(Index).Description
    End Function
    
    Private Function GetFileName(Index As Integer) As String
    Dim Nms As String
    
    Nms = Split(Trim(Cases(Index).NAME), " ")
    GetFileName = Nms(1) & ", " & Nms(0) & "_123456_" & Cases(Index).RecordNumber & " script.txt"
    End Function
    
    Sub SaveFiles()
    Dim x As Integer
        For x = 0 To UBound(Cases)
            Open GetFileName(x) For Output As #1
                Print #1, Cases(x)
            Close #1
        Next
    End Sub

    Quote Originally Posted by zynder
    Hey Andrew I was wondering where is the log written.
    There wasn't But its added now

  5. #5

    Thread Starter
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Parsing Experts!!! Need help.

    OK thanks. I'll try it out later. Cheers mate!

  6. #6
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Parsing Experts!!! Need help.

    Split(stream, vbCrLf & "CASE:") and you will get a string array eg strCases() with index 0 containing original header info.

    Redim another string array eg strHeader(Ubound(strCases)) for individual header of each CASE file.

    Populate strHeader() array {skip index zero and strHeader(1) is just the same as original header strCases(0)} in a way that strHeader(2) is the header of strCases(2), etc. You will get basic info from strCases(0) then add the per case file header info for the other strCases() array indices (headers for index 2 and above are the ones you need to create).

    So when you print the files, you print first from strHeader(index) then print strCases(index), don't forget to reinsert "CASE:", and that's in an iteration to Ubound() or index variable increments.

  7. #7
    Addicted Member kewakl's Avatar
    Join Date
    Oct 2006
    Location
    between keyboard and chair
    Posts
    220

    Re: Parsing Experts!!! Need help.

    Thank you, leinad31!
    Once again, I learned that I cannot trust Microsoft Documentation.
    MSDN says (Split(expression[, delimiter[, limit[, compare]]])

    delimiter Optional. String character used to identify substring limits. If omitted, the space character (" ") is assumed to be the delimiter. If delimiter is a zero-length string, a single-element array containing the entire expression string is returned.
    I assumed that since the delimiter description says that delimiter is a string character that it would have to be a SINGLE character.

    This nugget will save me a bit of programming the the future! (Rate+=1)
    Do not use if shrinkwrap is broken or missing!
    I'm learning how to fish, too!

  8. #8
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Parsing Experts!!! Need help.

    No prob

    The prepended vbCrLf ensures your only considering "CASE:" which is at the start of the line or not nested within the line, helps avoid per line read then test (eg. checking each line if it starts with "CASE:"), and gives you less array elements to process all of which simplifies things.
    Last edited by leinad31; Apr 12th, 2007 at 05:30 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width