Looping in text file

vb.net , textfile Kuala Lumpur, Malaysia
  • 11 years ago

    This are some lines from my text file 4.5 28 Red (T1 ) [1 12.86 53.8] X Y 198452 -1309 | 11.0 28 Red (2 13.10 04.0) [T1 ] 130611 2948 229111 28275 1.5 28 Blue (T2 ) [1 07.06 59.1] X Y 190456 39339 | 6.5 28 Blue (2 03.00 03.0) [T2 ] 132111 -67725 191611 -51225 2.0 28 Blue (T3 ) [2 10.04 23.4] X Y 101454 18441 | 4.0 28 Blue (2 15.50 36.0) [T3 ] 82611 19775 85111 -16725 2.0 28 Blue (T4 ) [1 10.00 45.8] X Y 210454 18691 | 14.0 28 Blue (2 08.00 24.0) [T4 ] 100611 -32725 227611 -16725 2.0 28 Blue (T5 ) [1 07.72 45.8] X Y 210454 34691 | 20.0 28 Blue (2 16.00 68.0) [T5 ] 34611 23275 211111 -51225 2.0 28 Blue (T6 ) [1 07.15 44.4] X Y 212454 38691 | 9.0 28 Blue (2 03.00 01.0) [T6 ] 135111 -67725 218611 -51225 2.0 28 Blue (T7 ) [1 07.43 45.8] X Y 210454 36691 | 9.0 28 Blue (2 03.50 01.0) [T7 ] 135111 -64225 217111 -51225 2.0 28 Blue (T8 ) [1 07.58 45.8] X Y 210454 35691 | 20.0 28 Blue (2 16.00 67.0) [T8 ] 36111 23275 209611 -51225 2.0 28 Blue (T9 ) [1 07.50 46.4] X Y 209454 36191 | 7.5 28 Blue (2 03.00 02.0) [T9 ] 133611 -67725 203611 -51225 2.0 28 Blue (T10 ) [1 07.51 59.1] X Y 190456 36138 | 6.5 28 Blue (2 03.50 03.0) [T10 ] 132111 -64225 190111 -51225 2.0 28 Blue (T11 ) [1 07.62 60.4] X Y 188485 35352 | 18.0 28 Blue (2 03.00 77.0) [T11 ] 21111 -67725 188611 -51225 2.0 28 Blue (T12 ) [1 07.62 77.6] X Y 162785 35352 | 4.0 28 Blue (2 03.00 04.0) [T12 ] 130611 -67725 163111 -51225 2.5 28 Blue (T13 ) [2 11.25 23.4] X Y 101454 9941 | 2.5 28 Blue (2 14.50 37.0) [T13 ] 81111 12775 79111 -6225 2.5 28 Blue (T14 ) [2 11.97 23.4] X Y 101454 4941 | 1.0 28 Blue (2 11.50 38.0) [T14 ] 79611 -8225 79111 -3225 2.5 28 Blue (T15 ) [2 12.54 23.4] X Y 101454 941 | 3.0 28 Blue (2 16.50 41.0) [T15 ] 75111 26775 79111 -225 2.5 28 Blue (T16 ) [2 11.11 22.8] X Y 102454 10941 | 3.0 28 Blue (2 15.50 41.0) [T16 ] 75111 19775 80611 -6225 2.5 28 Blue (T17 ) [2 11.54 21.4] X Y 104454 7941 | 1.5 28 Blue (2 11.00 31.0) [T17 ] 90111 -11725 82111 -4725 2.5 28 Blue (T18 ) [2 11.97 21.4] X Y 104454 4941 | 1.0 28 Blue (2 11.50 35.0) [T18 ] 84111 -8225 83611 -6225 2.5 28 Blue (T19 ) [2 11.97 20.8] X Y 105454 4941 | 1.0 28 Blue (2 11.50 36.0) [T19 ] 82611 -8225 83611 -4725 2.5 28 Blue (T20 ) [2 11.68 22.1] X Y 103454 6941 | 3.5 28 Blue (2 15.50 49.0) [T20 ] 63111 19775 83611 -3225 2.5 28 Blue (T21 ) [2 11.68 22.8] X Y 102454 6941 | 3.0 28 Blue (2 15.50 44.0) [T21 ] 70611 19775 83611 -1725 2.5 28 Blue (T22 ) [2 12.25 22.1] X Y 103454 2941 | 5.5 28 Blue (2 17.50 57.0) [T22 ] 51111 33775 85111 -1725 want to replace the X and Y column to user enter value.

    For example If User enter X=100 Y=100.

    So the text File Should Do changes Like this:

    In first line The X and Y should Be replace by 100. For the second line to the 10th line The X should X=X+2.54 and Y remain 100. For 11th line X=100 and Y=Y+2.54. From line 12 to 20 X=X+2.54 and Y remain which is 102.54. The patern should be continue until end of file. For every 10 line the Y value should change. For example My X and Y column Should Look like this:

    100 100 102.54 100 105.08 100 107.62 100 110.16 100 112.7 100 115.24 100 117.78 100 120.32 100 122.86 100 100 102.54 102.54 102.54 105.08 102.54 107.62 102.54 110.16 102.54 112.7 102.54 115.24 102.54 117.78 102.54 120.32 102.54 122.86 102.54

         Dim lines() As String = IO.File.ReadAllLines("C:\wirematches.txt")
        Dim xValue As Decimal = Val(TextBox1.Text)
        Dim yValue As Decimal = Val(TextBox2.Text)
        Dim altValue As Decimal = 2.54
        Dim lineTracker As Integer = 0
        Dim aX As Decimal
    
        For lineNumber As Integer = 0 To lines.GetUpperBound(0)
    
            Dim threeSections() As String = lines(lineNumber).Split("]")
            Dim twoSections() As String = threeSections(1).Split("|")
            Dim fourNumbers() As String = twoSections(0).Split()
    
    
    
            Dim counter As Integer = 0
            For i As Integer = 0 To fourNumbers.Count - 1
                If fourNumbers(i).ToString.Length <> 0 Then
                    fourNumbers(counter) = fourNumbers(i)
                    counter += 1
                End If
            Next
    
            If lineTracker = 0 Then
                aX = xValue
                fourNumbers(0) = aX.ToString("0.00")
                fourNumbers(1) = yValue.ToString("0.00")
            ElseIf (lineTracker = Val(TextBox6.Text)) Then
                aX = xValue
                yValue = yValue + altValue
                fourNumbers(0) = aX.ToString("0.00")
                fourNumbers(1) = yValue.ToString("0.00")
            ElseIf lineTracker > 0 And lineTracker < Val(TextBox6.Text) Then
                aX = aX + altValue
                fourNumbers(0) = (aX).ToString("0.00")
                fourNumbers(1) = (yValue).ToString("0.00")
    
            End If
            If lineTracker < Val(TextBox6.Text) Then
                lineTracker += 1
    
            Else
                lineTracker = 1
            End If
    
    
            Dim whitespace As Integer = 8 - fourNumbers(3).ToString.Length
            Dim spacer As String = ""
            Do Until whitespace = 0
                spacer = spacer & " "
                whitespace -= 1
    
            Loop
            lines(lineNumber) = threeSections(0) & "]  " & fourNumbers(0) & "  " & fourNumbers(1) & "  " & fourNumbers(2) & "  " & fourNumbers(3) & spacer & "|" & twoSections(1) & "]" & threeSections(2)
        Next
    
    
        IO.File.WriteAllLines("C:\wirematchesout.txt", lines)
    

    This above code replace all the line.

    My problem now is I just want to replace the lines which is specify by the user. For example if user enter Row=2 and column=10 then only 20 lines should be changes. other lines should remain with x and Y

    For row =2 and column=10 The file should be like this 100 100 102.54 100 105.08 100 107.62 100 110.16 100 112.7 100 115.24 100 117.78 100 120.32 100 122.86 100 100 102.54 102.54 102.54 105.08 102.54 107.62 102.54 110.16 102.54 112.7 102.54 115.24 102.54 117.78 102.54 120.32 102.54 122.86 102.54

    Other lines remain unchange>

    Can any one help me pls

    Im using vb.net

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Never trust a programmer in a suit.” - Anonymous