Question Please help having a problem with nested loops

brtm5

Member
Joined
May 10, 2012
Messages
5
Programming Experience
Beginner
Hi everyone, I could really use some help. I'm writing a function that is supposed to read in a file and split off chunks of that file to a new file. So basicly whenever the program sees the word <color> it is to write that line out and every line after it until it hits </color>. It should then write the </color> line out then wait until the next instance of the word <color> and then repeat the process.
I hope this makes sense. Here is copy of my latest attemp at coding this. Thank you for your help!

Public Function colors(ByVal Inputfile() As String, ByVal count As String) As String
    Dim match As String
    Dim aryText(count) As String'// set the counter in the string
    aryText = Inputfile '// assigning the file to the array that will be written out.
    Dim FILE_NAME As String = ("C:\test.txt")
    Dim objWriter As New System.IO.StreamWriter(FILE_NAME) '//set the name of the file to be written out
    For i = 0 To count - 1 '// loop to scan file
        Do Until aryText(i).Trim = "</Color>"
            match = aryText(i)
            objWriter.WriteLine(aryText(i))
            i = i + 1
        Loop
        If aryText(i).Trim = "</Color>" Then
            match = aryText(i)
            objWriter.WriteLine(aryText(i))
        End If
    Next
    Return count
End Function
 
Last edited by a moderator:
I would approach like this, this is high level pseudo code:
Declare a Boolean variable with initial value False.
Loop for each line in lines. Inside loop:
If line is <Color> set variable to True.
Write line if variable is True.
If line is </Color> set variable to False.​
 
Nested Loops Answered!

Thank JohnH for your help. That helped point me in the right direction.
Here is the final working code.
Thanks again
Public Function colors(ByVal Inputfile() As String, ByVal count As Integer) As String
    Dim match As String
    Dim aryText(count) As String'// set the counter in the string
    aryText = Inputfile '// assigning the file to the array that will be written out.
    Dim FILE_NAME As String = ("C:\test.txt")
    Using writer As StreamWriter = New StreamWriter(FILE_NAME)
        Dim i As Integer = 0
        For i = 0 To newstr_count - 1
            If aryText(i).Trim = "<Color>" Then
                Do Until aryText(i).Trim = "</Color>"
                    match = aryText(i)
                    writer.WriteLine(aryText(i), i) ' Write formated string
                    i = i + 1
                Loop
                If aryText(i).Trim = "</Color>" Then
                    match = aryText(i)
                    writer.WriteLine(aryText(i), i) ' Write formated string
                End If
            End If
        Next i
    End Using
    Return match
End Function
 
Last edited by a moderator:
Declare a Boolean variable with initial value False.
Loop for each line in lines. Inside loop:
If line is <Color> set variable to True.
Write line if variable is True.
If line is </Color> set variable to False.​
here is the pseudo code translated to VB code:
        Dim write As Boolean = False
        For Each line In Inputfile
            If line.Trim = "<Color>" Then write = True
            If write Then writer.WriteLine(line)
            If line.Trim = "</Color>" Then write = False
        Next

The Trim calls may not be necessary, though your file format is unknown.
 
Back
Top