parsing a line of text

cfisher440

Well-known member
Joined
Oct 11, 2005
Messages
73
Programming Experience
1-3
I am driving myself crazy attempting to come up with a function that will do this. I am trying to parse a line. Easy enough of a task, but the lines look like this.

VB.NET:
013 11:08    62     1     6     6     1  04:09 00:01 05:08 01:25 02:20    1.0  55.3
020 08:21    31     0     1     0     0  11:22 02:00 00:00 07:34 00:00    4.0  11.2

Notice that
1.) the integers and time values are spaced apart at different lengths from each other (with the use of spaces (as opposed to tabs)).
2.) all lines are formatted like this (but where there is a value like 6, there could be a 23 (a tens value as oppossed to a ones value).

I have come across a function that will recieve the integer values out of a line of text, but for some reason it won't extract the time values correctly.

Here is the function:

VB.NET:
Private Function GetData(ByVal LineOfText As String, ByVal DataNumber As Int16) As String
        '*********************************************************************************************
        '*** Feed the function a line of text as a string.
        '*** and feed it a number. It will return the word within the sentence.
        '*** Ex:
        '*** This is a line of text
        '*** if that line of text was a string named test and I fed the function test and 1 I would 
        '*** recieve the second word of the sentence. Thus, the value returned will be "is". 
        '*********************************************************************************************
        Dim currNumber As Int16 = -1
        Dim sData As String
        Dim blnSpaces As Boolean
        For i As Int16 = 0 To LineOfText.Length - 1
            If LineOfText.Chars(i) = " " And blnSpaces = False Then
                blnSpaces = True
            ElseIf LineOfText.Chars(i) = " " And blnSpaces = True Then
            ElseIf LineOfText.Chars(i) <> " " And blnSpaces = True Then
                blnSpaces = False
                currNumber += 1
            ElseIf LineOfText.Chars(i) <> " " And blnSpaces = False Then
            End If
            If currNumber = DataNumber And blnSpaces = False Then
                sData &= LineOfText.Chars(i)
            End If
        Next
        WriteData(sData)
        Return sData
    End Function
I have also used the regex.split function, which works nicely, but haven't found a way to make it work in this case when the lines will vary a lot with different values (some values being ones, some tens, etc..).

Any ideas for how to get the values of these line or how to adjust the function to do so, I would appreciate it.
 
I suppose to make a some experiment on this but i won't as i would never try to resolve this as you try. But, i would rather change the way how the data has been writen down to the file.
However, if you are forced to use it as it is i guess you need some simple function that will get under consideration all spaces as all of your values are consistent ... let me know if you still need some help to make this functionality and if you still don't want to change the way how the data are writen.

Regards ;)
 
Data layout can't be changed

Unfortunately, the way the text is layed out on the lines can't be changed.
So it needs to be parsed as it is layed out above.

I would still like help in finding a solution on how to parse the information out of these lines. Just remember that they are not consistent.

Example of how they are not consistent:
VB.NET:
001 15:45    39     0    14     0     1  04:52 37:03 00:00 00:45 00:00    7.4   0.0
020 08:21    31     0     1     0     0  11:22 02:00 00:00 07:34 00:00    4.0  11.2
the inconsistency in the format is the 14 in the first row and the 1 in the second row. This seems to be a problem for me. doing a regex.split function and parsing it by spaces I don't think will work due to that inconsistency.

Any ideas/comments/examples/things for sale (J/K) would be appreciated.
 
14 and 01 is consistent ;) you are dealing with fixed sized (and positioned) fields for every line
 
right i meant exactly about that ... 1 is one value as well as 14 is ... so if 1 is separated from zero and 14 is separated from zero as well i suppose we have consistent values here. However, i think we should stop to talk about it and find solution. Give me some time (i need to finish some other businesses here) and i'll prepare an example project that is acomplishing spliting ....
 
This won't be exactly what you want but you should be able to adapt it easily enough:
VB.NET:
        Dim line As String = sr.ReadLine() 'Get the next line from the file.
        Dim components As New Specialized.StringCollection

        While line <> String.Empty
            components.Add(line.Substring(0, line.IndexOf(" "c))) 'Get all characters up to the first space.
            line = line.Substring(line.IndexOf(" "c)) 'Remove all characters up to the first space.
            line.TrimStart() 'Remove all leading spaces.
        End While
 
Back
Top