Results 1 to 21 of 21

Thread: [RESOLVED] Help parsing a non-delimited text file

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    5

    Resolved [RESOLVED] Help parsing a non-delimited text file

    I have a text file that I need to export into excel or a comma delimited file. The file consists of a solid string of prices consisting of numbers and decimal points. The only way I can think to break it down is by finding the decimal point in the string advancing 2 digits for the cents(the price is not a fixed length throughout the file) parse it and keep repeating. Also the information represents columnar data,(i.e. there are 7 fields so after each 7th price a new line should be started to build the columns required. I'm sure this is quite simple but I've been away from programming too long for it to click. Any help is appreciated!
    Attached Files Attached Files

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Help parsing a non-delimited text file

    Are there always 2 digits after the decimal?

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    5

    Re: Help parsing a non-delimited text file

    Yes there are always 2 digits for the cents field. I posted the data file in the original post

  4. #4
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    958

    Re: Help parsing a non-delimited text file

    I looked at the file. What is the record length? 64, 128, 256, other?
    If you know this...no problem

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    5

    Re: Help parsing a non-delimited text file

    The record length is unknown. However the data is taken from a document that is 7 columns across by 150 rows.

  6. #6
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Help parsing a non-delimited text file

    I can write a parse routine for it but you'll have to wait a bit because I have to reload Visual Basic back onto my computer.

    Unless someone else comes up with the code before then.

  7. #7

  8. #8
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    958

    Re: Help parsing a non-delimited text file

    maybe
    3.80|3.88|4.00|4.16|4.38|4.45|4.

  9. #9

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    5

    Re: Help parsing a non-delimited text file

    Sessi4ml,
    your data line;
    3.80|3.88|4.00|4.16|4.38|4.45|4.
    is correct except for the last column(7). It should read
    4.56 instead of (4.). Then the next data (3.84) goes back to column 1 and loops like this until done.
    3.84|4.07|4.47|4.54|4.88|5.00|5.30

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

    Re: Help parsing a non-delimited text file

    prepend "00" to string
    split on "." and store in string array
    resize a dynamic destination currency array to size ubound(string array) + Iff(len(last string array element)> 2, 1, 0) -1
    loop through string array from lbound to ubound - 1
    Mid(current string array element, 3) contains whole number, convert to currency and assign to currency array. BTW Mid(str, 3) is reason for prepend of "00"
    Left(next array element, 2) contains decimal portion. Convert to num and divide by 100 then add to whole number in currency array.
    Special handling for last array element, if len > 2 then theres still one more whole number without decimal point

    EDIT: Based this post on 3.80|3.88|4.00|4.16|4.38|4.45|4If decimal points are complete then no special handling or last array element needed. And currency array size is ubound string array - 1
    Last edited by leinad31; Jan 4th, 2007 at 11:16 PM.

  11. #11
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Help parsing a non-delimited text file

    I think I have an idea. How about a generic function for this kind of job?

    VB Code:
    1. ' in a module
    2. Option Explicit
    3.  
    4. Public Function Clip(ByRef Text As String, ByRef Keyword As String, ByVal CharPadding As Byte) As String()
    5.     Static lngEnd() As Long, strOut() As String
    6.     Dim lngEndUB As Long, lngPos As Long, lngCount As Long
    7.     ' error detection
    8.     If LenB(Text) = 0 Or LenB(Keyword) = 0 Then Exit Function
    9.     ' reserve space
    10.     lngEndUB = 40
    11.     ReDim Preserve lngEnd(lngEndUB)
    12.     ' find first
    13.     lngPos = InStr(Text, Keyword)
    14.     Do Until lngPos < 1
    15.         ' correct end position
    16.         lngPos = lngPos + CharPadding
    17.         ' need more space?
    18.         If lngEndUB < lngCount Then
    19.             lngEndUB = lngEndUB * 2
    20.             ReDim Preserve lngEnd(lngEndUB)
    21.         End If
    22.         ' remember
    23.         lngEnd(lngCount) = lngPos
    24.         ' jump to next
    25.         lngCount = lngCount + 1
    26.         ' find next
    27.         lngPos = InStr(lngPos + 1, Text, Keyword)
    28.     Loop
    29.     ' decrease as we have one too many
    30.     lngCount = lngCount - 1
    31.     ' none?
    32.     If lngCount < 0 Then Exit Function
    33.     ' reserve space for strings
    34.     ReDim Preserve strOut(lngCount)
    35.     ' validate last item is not out of string length
    36.     If lngEnd(lngCount) > Len(Text) Then lngEnd(lngCount) = Len(Text)
    37.     ' get first item
    38.     strOut(0) = Left$(Text, lngEnd(0))
    39.     ' get remaining items
    40.     For lngPos = 1 To lngCount
    41.         ' clip the one we need
    42.         strOut(lngPos) = Mid$(Text, lngEnd(lngPos - 1) + 1, lngEnd(lngPos) - lngEnd(lngPos - 1))
    43.     Next lngPos
    44.     ' return the final string array
    45.     Clip = strOut
    46. End Function
    VB Code:
    1. ' sample project: add listbox and command button
    2. Private Sub Command1_Click()
    3.     Dim strTest As String, strIn() As String, lngA As Long
    4.     strTest = "3.803.884.004.164.384.45"
    5.     strIn = Clip(strTest, ".", 2)
    6.     For lngA = 0 To UBound(strIn)
    7.         List1.AddItem strIn(lngA)
    8.     Next lngA
    9. End Sub

  12. #12
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Help parsing a non-delimited text file

    I came up with a quick sub, didn't test it that much but I think it's working. (Cross-checking with the data file is hard on the eyes )

    VB Code:
    1. Private Sub LoadData(ByVal FilePath As String, LVObject As Object, ByVal Columns As Integer)
    2.     Dim lonLoop As Long 'Loop counter.
    3.     Dim intCol As Integer 'Current column we're on.
    4.     Dim lonPos As Long 'Current position of decimal.
    5.     Dim lonPrevDotPos As Long 'Previous position of decimal.
    6.     Dim lonNextDotPos As Long 'Decimal position after current decimal.
    7.     Dim intFF As Integer 'File handle.
    8.     Dim strData As String 'File data.
    9.     Dim intRow As Integer 'Current row.
    10.     Dim intPrevRow As Integer 'Previous row.
    11.     Dim strCur As String
    12.     Dim lonStart As Long
    13.     Dim lonEnd As Long
    14.    
    15.     intFF = FreeFile
    16.    
    17.     Open FilePath For Binary Access Read As #intFF
    18.        
    19.         If LOF(intFF) = 0 Then
    20.             Close #intFF
    21.             Exit Sub
    22.         Else
    23.             strData = Space$(LOF(intFF))
    24.             Get #intFF, 1, strData
    25.         End If
    26.    
    27.     Close #intFF
    28.    
    29.     intRow = 1
    30.     lonPos = InStr(1, strData, ".", vbBinaryCompare)
    31.    
    32.     With LVObject
    33.         .ListItems.Clear
    34.        
    35.         'Keep looping while there are still decimals.
    36.         Do While lonPos > 0
    37.             'Increase current column.
    38.             intCol = intCol + 1
    39.            
    40.             If intCol > Columns Then
    41.                 intPrevRow = intRow
    42.                 intRow = intRow + 1
    43.                 intCol = 1
    44.             End If
    45.            
    46.             If lonPrevDotPos = 0 Then
    47.                 'We're on the first decimal.
    48.                 'Get current value.
    49.                 strCur = Mid$(strData, 1, lonPos + 2)
    50.                 lonPrevDotPos = lonPos
    51.                 lonPos = lonPos + 2
    52.             Else
    53.                
    54.                 'This is not the first decimal.
    55.                 'Get text from previous decimal + 3
    56.                 'to this decimal + 2
    57.                 lonStart = lonPrevDotPos + 3
    58.                 lonEnd = lonPos + 2
    59.                
    60.                 'strCur = Mid$(strData, lonPrevDotPos + 3, (lonPos + 2) + (lonPrevDotPos + 3) - 2)
    61.                 strCur = Mid$(strData, lonStart, (lonEnd - lonStart) + 1)
    62.                 lonPrevDotPos = lonPos
    63.                 lonPos = lonPos + 2
    64.             End If
    65.            
    66.             If Len(strCur) > 0 Then
    67.                
    68.                 If intCol = 1 Then
    69.                     .ListItems.Add , , strCur
    70.                 Else
    71.                     .ListItems(intRow).ListSubItems.Add , , strCur
    72.                 End If
    73.            
    74.             End If
    75.        
    76.             lonPos = InStr(lonPos + 1, strData, ".", vbBinaryCompare)
    77.         Loop
    78.    
    79.     End With
    80.    
    81.     strData = ""
    82. End Sub
    83.  
    84. Private Sub Command1_Click()
    85.     LoadData App.Path & "\data.txt", lvData, 7
    86. End Sub

    I also attached a sample project.
    Last edited by DigiRev; Mar 26th, 2007 at 02:55 AM.

  13. #13
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Help parsing a non-delimited text file

    And of course Merri posts something half the size of mine and probably twice as fast.

  14. #14
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    958

    Re: Help parsing a non-delimited text file

    Maybe I am wrong, or maybe old school
    VB Code:
    1. Dim strData As String
    2. '3.80|3.88|4.00|4.16|4.38|4.45|4.
    3. 'is correct except for the last column(7). It should read
    4. '4.56 instead of (4.). Then the next data (3.84) goes back to column 1 and loops like this until done.
    5. '3.84|4.07|4.47|4.54|4.88|5.00|5.30
    6.  
    7. Private Sub Form_Load()
    8. Dim inrStart As Integer: Dim intX As Integer
    9.     strData = "3.803.884.004.164.384.454.563.844.074.474.544.885.005.303.874.264.704.855.225.395.883.984.414.965.175.525.706.264.254.495.205.415.776.016.644.324.685.295.585.966.256.874.574.825.425."
    10.     intStart = 1
    11.     For intStart = 1 To Len(strData)
    12.     intX = InStr(intStart, strData, ".")
    13.      ' x points to the dot
    14.      'start+ dot +2..1+2+2 .. start + x +2
    15.      
    16.     Text1.Text = Text1.Text + Mid(strData, intStart, intX + 3 - intStart) + ","
    17.     a = a + 1: If a = 7 Then a = 0: Text1.Text = Text1.Text + vbCrLf
    18.     intStart = intX + 2
    19.     Next
    20.    
    21. End Sub

    My results:
    3.80,3.88,4.00,4.16,4.38,4.45,4.56,
    3.84,4.07,4.47,4.54,4.88,5.00,5.30,
    3.87,4.26,4.70,4.85,5.22,5.39,5.88,
    3.98,4.41,4.96,5.17,5.52,5.70,6.26,
    4.25,4.49,5.20,5.41,5.77,6.01,6.64,
    4.32,4.68,5.29,5.58,5.96,6.25,6.87,
    4.57,4.82,5.42,5.,

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

    Re: Help parsing a non-delimited text file

    Ooops by mistake, thought it was data needed to be converted to numeric for immediate use...
    If your just gonna format the string to include comma separators...

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.    MsgBox Parse("3.803.884.004.164.384.454.563.844.074.474.544.885.005.303.874.264.704.855.225.395.883.98")
    5. End Sub
    6.  
    7. Public Function Parse(ByVal SrcString As String) As String
    8. Dim sarTmp() As String
    9. Dim cnt As Long
    10.  
    11.    sarTmp = Split(SrcString, ".")
    12.    For cnt = 1 To UBound(sarTmp) - 1
    13.       sarTmp(cnt) = Left(sarTmp(cnt), 2) & "," & Mid(sarTmp(cnt), 3)
    14.    Next
    15.    Parse = Join(sarTmp, ".")
    16. End Function

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

    Re: Help parsing a non-delimited text file

    Quote Originally Posted by sessi4ml
    Maybe I am wrong, or maybe old school
    VB Code:
    1. Dim strData As String
    2. '3.80|3.88|4.00|4.16|4.38|4.45|4.
    3. 'is correct except for the last column(7). It should read
    4. '4.56 instead of (4.). Then the next data (3.84) goes back to column 1 and loops like this until done.
    5. '3.84|4.07|4.47|4.54|4.88|5.00|5.30
    6.  
    7. Private Sub Form_Load()
    8. Dim inrStart As Integer: Dim intX As Integer
    9.     strData = "3.803.884.004.164.384.454.563.844.074.474.544.885.005.303.874.264.704.855.225.395.883.984.414.965.175.525.706.264.254.495.205.415.776.016.644.324.685.295.585.966.256.874.574.825.425."
    10.     intStart = 1
    11.     For intStart = 1 To Len(strData)
    12.     intX = InStr(intStart, strData, ".")
    13.      ' x points to the dot
    14.      'start+ dot +2..1+2+2 .. start + x +2
    15.      
    16.     Text1.Text = Text1.Text + Mid(strData, intStart, intX + 3 - intStart) + ","
    17.     a = a + 1: If a = 7 Then a = 0: Text1.Text = Text1.Text + vbCrLf
    18.     intStart = intX + 2
    19.     Next
    20.    
    21. End Sub

    My results:
    Don't do concatentaion for the entire stream... that's slow cause memory has to be repeatedly set aside and assigned for growing stream.

  17. #17
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    958

    Smile Re: Help parsing a non-delimited text file

    Don't do concatentaion for the entire stream... that's slow cause memory has to be repeatedly set aside and assigned for growing stream.
    True, looking at a 5k file , and this is version 1, and I am new this VB stuff
    next time I'll be better

  18. #18
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Help parsing a non-delimited text file

    Quote Originally Posted by DigiRev
    And of course Merri posts something half the size of mine and probably twice as fast.
    You forgot to mention I coded it in half the time as well!

    However, I didn't provide a fully ready to go solution, but hopefully it is a function that'll be of value for atleast someone else reading this thread later on.

  19. #19
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Help parsing a non-delimited text file

    Quote Originally Posted by Merri
    You forgot to mention I coded it in half the time as well!
    Yea, well half that time I was re-installing VB.

  20. #20

  21. #21
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    958

    Re: [RESOLVED] Help parsing a non-delimited text file

    Scooby, here is the Text to Excel.
    Good to hear the code worked
    Any questions, just ask.

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