-1

I am using vb.net to read a text file and see if a record matched, and to pick out that record and write it to excel.

However with the peek loop it reads the file until the end of it. I have 2 peek loop - the outer peek loop looks for the item, the inner loop search to find it.

Can the peek loop stop, and continue where the outer loop stop ? This is an example of my code. I did a for loop outside both search loop to continue where the outer loop stops however is not right that way either. Thanks

 For i = 1 To t_idex

            rder.BaseStream.Seek(0, IO.SeekOrigin.Begin)
            rder.DiscardBufferedData()

            Do While rder.Peek() >= 0
                line = rder.ReadLine()
                r_idx += 1
                st_outer += 1

                m = Mid(line, 3, 7)
                P = Mid(line, 19, 7)
                b= Trim(Mid(line, 34, 14))

                If Len(b) = 14 Then
                    Do While rder.Peek() >= 0
                        line2 = rder.ReadLine()

                        m2 = Mid(line2, 3, 7)
                        P2 = Mid(line2, 19, 7)
                        b2 = Trim(Mid(line2, 34, 14))

                        If b = b2 Then
                            cnt_dups += 1

                        End If

                        r_idx += 1
                    Loop
                End If
                i = st_outer
            Loop

        Next
Johnseito
  • 315
  • 1
  • 9
  • 24
  • Can you explain better what you want to achieve here? Where is the code that finds something? The inner loop contine to read until the end of the file, there is no condition to stop that loop if you find something. – Steve Jan 14 '17 at 20:52
  • Why complicate this? Read a line, see if it is the one you want, if so exit the loop, or until you reach the end of file. – dbasnett Jan 14 '17 at 20:56
  • @steve I updated my code. The outer loop will find that len of b is 14, if so go into inner loop, loop through the entire file from where the outer loop find len of b is 14, and when there is a match on b to b2, write both records to excel (code not update to do this yet). Do this until end of file. What happen after that point is that it runs and completely out of both loop when ideally is supposed to go to the outer loop and stop where it left off and increment and do the next search until the outer loop is complete to eof. Hope you know what I mean. – Johnseito Jan 14 '17 at 22:01
  • @Johnseito There could be a simpler way to process the file: you might want to look at the [TextFieldParser class](https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser(v=vs.110).aspx). – Andrew Morton Jan 14 '17 at 22:02
  • @dbasnett after finding what I want, I don't just want to exit the loop because what if there are two records that match, we won't know until the inner loop checks to end of file. After inner loop checks to end of file, it should continue to the records where the outer loop stop to continue checking. – Johnseito Jan 14 '17 at 22:03
  • @ Andrew Morton thanks, I am going to look into the TextFieldParser class and see what I can do. I am new to vb. – Johnseito Jan 14 '17 at 23:09
  • @ Andrew Morton sorry I checked, still doesn't answer my question. How can I use the outer loop to continue where it left off when it went into the inner loop ? Thanks – Johnseito Jan 15 '17 at 01:38

1 Answers1

1

See what this does for you....

Dim Lines() As String = File.ReadAllLines("SomeTextFile.txt")

For LineIndex = 0 To Lines.GetUpperBound(0)
    Dim Match1 As String = Mid(Lines(LineIndex).Trim, 34, 14)
    If Match1.Length = 14 Then
        For Each RemainingLine As String In Lines.Skip(LineIndex + 1)
            Dim Match2 As String = Mid(RemainingLine.Trim, 34, 14)
            If Match1 = Match2 Then
                ' We have a duplicate...
                DoSomething()

                ' Resume outer loop
                ' Comment this line to find all duplicates
                ' Uncomment this line to short-circuit exit upon finding
                '     the first duplicate
                Exit For
            End If
        Next
    End If
Next

References:

MrGadget
  • 1,258
  • 1
  • 10
  • 19
  • thank you for providing the code example. It looks beautiful and is just what I am looking for. One thing I've changed is that when I noticed your code doesn't check till the end of the file because of the exit for, so I took that out, and put a stop variable in the outer loop to see where it stop and have LineIndex continue from there. Nevertheless it was a GREAT code example, very helpful. – Johnseito Jan 17 '17 at 02:30
  • Ok, I'm curious what exactly you changed, because I could interpret your comment a couple of ways and I'd like to understand what you meant. – MrGadget Jan 17 '17 at 02:40
  • I only added three lines and took out one, under the Dim lines() very top, I added Dim stp_idx as Integer = 0, in the For LinIndex = 0 under Dim match1, I added stp_idx += 1, in the match1 = match2, I took out the exit For and after the Next code of the inner loop I added LineIndex = stp_idx -1. I did this so that the inner loops goes over to the end of file just in case it finds another match after the first match, and the outer loop then picks up where it left off. :) – Johnseito Jan 17 '17 at 03:07
  • Taking out the Exit For will let it find all duplicates, instead of bailing out of the inner loop on finding the first one. Based on what you describe, if you debug your `stp_idx` you'll find it has no effect at all, regardless of whether duplicates are found. `LineIndex` is already holding its position while the inner loop runs...it doesn't need to be reset to where it left off. – MrGadget Jan 17 '17 at 03:14
  • I just checked, and you are right. Thank you for the GREAT code. It's even better, and is just what I am looking for w/o many edit or additional code. It's just perfect. – Johnseito Jan 17 '17 at 03:25