|
-
Feb 21st, 2009, 06:02 PM
#1
Thread Starter
Addicted Member
[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.
-
Feb 21st, 2009, 06:09 PM
#2
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
-
Feb 21st, 2009, 06:31 PM
#3
Thread Starter
Addicted Member
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.
-
Feb 21st, 2009, 06:51 PM
#4
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
-
Feb 21st, 2009, 07:00 PM
#5
Thread Starter
Addicted Member
Re: Parsing Text
LaVolpe, thanks for your help!! I'm good to go.
-
Feb 21st, 2009, 07:04 PM
#6
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|