End of File & block of text parsing

rgouette

Member
Joined
May 22, 2006
Messages
16
Location
Maine,USA
Programming Experience
1-3
So, I'm looping a readline function against blocks of text, and wish to detect when I've reached the end of my file.
A sample of two blocks of the text I'm parsing is below.
I'm using VS 2003


DATE: TUE 11/15/2005 4:21:48 PM
INITIALS: PRT
CMTAG: BLADE1
DEPARTMENT: SCU
CAMPUS: 22Maple
BUILDING: Bldg1
FLOOR: GRND
ROOM:
JACK:
LOC: SCU,22BRAM,BEAN,GRND,,
RC: SCU.NURSING.22BRAM.MMDORG
TYPE: Generic

DATE: TUE 11/15/2005 3:03:24 PM
INITIALS: PRT
CMTAG: BLADE2
DEPARTMENT: R6
CAMPUS: 24 East
BUILDING: Bldg3
FLOOR: 6
ROOM:
JACK:
LOC: R6,22BRAM,RICH,6,,
RC: NURSING.22BRAM.MMDORG
TYPE: Clinical

What I wish to do is, enable a search pattern that can search through the entire block of text, and if there's a match, write that block of text toa listbox.
So, th euser might search on "24 East", in which case I would want to write the ENTIRE block that contains that value.
So, my difficulty is in creating a way to search the whole block of text, or search each line IN a block, but keep the block together & write as needed.

see code below for what I have so far.

Confusing?
Thanks lads,
Rich

VB.NET:
     'Loop through the file until the end is reached
        Do While Not myreader.Peek = -1

            'Read the current line into the currentLine variable
            currentLine = myreader.ReadLine

            'Determine if any of the words the user would like
            'to find are in the currentLine variable
            'Before doing so however, store the currentLine in
            'lower case letters to aid in searching (case sensitive)
            lowerCaseLine = currentLine.ToLower

            For Each word As String In wordsToSearchFor
                If lowerCaseLine.IndexOf(word) Then
                    'Add this currentLine variable to your listbox
                    lb_1.Items.Add(currentLine)

                    'Don't bother searching for more words as one
                    'has already been found in this currentLine
                    Exit For
                End If
            Next

        Loop
 
This example does not display the best coding practices, but gets the job done :)
VB.NET:
Public Class record
    Public sDATE, sINITIALS, sCMTAG, sDEPARTMENT, sCAMPUS, sBUILDING, sFLOOR, sROOM, sJACK, sLOC, sRC, sTYPE As String
    Public Sub setvalue(ByVal member As String, ByVal value As String)
        Me.GetType.GetField("s" & member).SetValue(Me, value)
    End Sub
    Public Function ToArray() As ArrayList
        Dim al As New ArrayList
        For Each f As Reflection.FieldInfo In Me.GetType.GetFields()
            al.Add(f.Name.Substring(1) & ": " & f.GetValue(Me))
        Next
        Return al
    End Function
End Class
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
    Dim sr As New IO.StreamReader("records.txt")
    Dim line(), name, value As String, rec As New record, found As Boolean
    Dim search As String = "BLADE".ToLower
    While sr.Peek <> -1
        line = sr.ReadLine.Split(":")
        If line.Length = 1 Then
            If found = True Then Exit While
            rec = New record
        Else
            name = line(0).Trim
            value = line(1).Trim
            rec.setvalue(name, value)
            If value.ToLower.[SIZE=2]IndexOf(search) <> -1 [/SIZE]Then found = True
        End If
    End While
    sr.Close()
    ListBox1.DataSource = rec.ToArray
End Sub
 
Back
Top