Results 1 to 6 of 6

Thread: [RESOLVED] Parsing Text

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    250

    Resolved [RESOLVED] Parsing Text

    Hi All,

    I really need your help on parsing the below three lines of text:

    Code:
         Dirs :      1225         0      1225         0         0         2
        Files :      9996        18      9976         0         2         3
        Bytes :   7.066 g    6.32 m   6.617 g         0  453.62 m       272
    I need to pull the numbers from each row and populate them into an array.

    For example on the first line, I would like to have an array that would be filled with the 6 numbers, 1225, 0, 1225, 0, 0, 2.

    The problem I am having is that the spaces are not uniform from line to line and I cannot parse or split the lines on a static string.

    Can anyone offer some assistance? Thank you all in advance. Please let me know if I need to clarify anything.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Parsing Text

    Try pre-formatting the line before splitting it on spaces (assuming they are spaces and not tabs or some other character:
    Code:
    Do Until InStr(theLine, "  ") = 0 ' < double space
        theLine = Replace$(theLine, "  ", " ") ' < make double spaces single spaces
    Loop
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    250

    Re: Parsing Text

    LaVolpe,

    That works great for the first two lines, excellent idea! Do you propose anything for the third line where the "space" then "letter" must be applied to the same array object?

    For example, "7.066 g", "6.32 m" , "6.617 g", and "453.62 m" must be applied to the same array object.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Parsing Text

    That could be a bit more tricky. If you notice that when 0 is present, you don't get the letter. You might want to handle that one a little differently
    Code:
    Dim sTokens() As String, I As Integer, arrayPtr As Long
    ... pre-format the text with the Do Loop
    ... adjust startPos to where you would normally start processing the split
    sTokens() = Split(theLine, " ")
    arrayPtr = -1 ' ? the position where the size will be added to the next array item, minus one
    For I = startPos To UBound(sTokens)
        If IsNumeric(sTokens(i)) = True Then 
            arrayPtr = arrayPtr + 1
            yourArray(arrayPtr) = sTokens(i)
        Else ' probably a letter, no?
             yourArray(arrayPtr) = yourArray(arrayPtr) & sTokens(i) ' append letter
        End If
    Next
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    250

    Re: Parsing Text

    LaVolpe, thanks for your help!! I'm good to go.

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: [RESOLVED] Parsing Text

    You're welcome. Strongly recommend, if not already done, adding some error checking. What if the text gets garbled and there are not the same number of columns per line? What if you are missing a prefix of Dirs, Files, Bytes? The latter is easy to check, read the 3 lines first before processing them, check to see if they exist. The former is a bit more complicated and one idea is to cache the number of columns when the Dirs line is processed. If the Files and/or Bytes lines do not come up with the same number of columns, then roll back/undo the array changes and print to an error log. Just some thoughts.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

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