简体   繁体   中英

Having trouble with an algorithm that loops through a string

I am using Visual Studio Community.
Hi, I wrote a loop that loops through a string of lottery drawings.I would like to grab a full drawing and cut out the date, drawings and the Powerball numbers.

Everything works well the first time through the loop, and I get 06/29/2002 04 33 35 36 45 Powerball: 22 , but the second time through the loop I get 07/03/2002 " 04 33 35 36 45 " Powerball: 22.

The second time through the loop it goes back to the numbers for the first drawing. I know I could write

Numbers(i) = Full_History.Substring(Start + 10, 16) , and Balls(i) = FullHistory.Substring(Start + 26, 14) , but that doesn't seem right. I hope I explained this right. Any help would be greatly appreciated.

Dim Start As Integer = 0                                                'Create a variable for start
Dim Finish As Integer = 40                                              
For i As Integer = 0 To Array_Size - 1 Step 1                           'Loop through the string
Full_Draw(i) = Full_History.Substring(Start, Finish)                'Store the full drawing
Dates(i) = Full_History.Substring(Start, 10)                        'Store the Date of the drawing
Numbers(i) = Full_History.Substring(10, 16)                         'Store the numbers drawn
Balls(i) = Full_History.Substring(26, 14)                           'Store the 'ball' if necessary
Start += 40                                                         'Increment the start variable

If you have such specialized text you can use a regular expression to extract the data, like in this snippet:

Dim rx As New System.Text.RegularExpressions.Regex("(\d\d\/\d\d\/\d{4})\s(\d\d)\s(\d\d)\s(\d\d)\s(\d\d)\s(\d\d)\s+Powerball:\s(\d\d)")
Dim s = "06/29/2002 04 33 35 36 45  Powerball: 22 06/29/2002 04 33 35 36 45  Powerball: 22 06/29/2002 04 33 35 36 45  Powerball: 22 06/29/2002 04 33 35 36 45  Powerball: 22 06/29/2002 04 33 35 36 45  Powerball: 22"
Dim matches = rx.Matches(s)
Dim sb As New System.Text.StringBuilder
For Each m As System.Text.RegularExpressions.Match In matches
    sb.AppendLine("Date: " & m.Groups(1).Value)
    sb.AppendLine("#1: " & m.Groups(2).Value)
    sb.AppendLine("#2: " & m.Groups(3).Value)
    sb.AppendLine("#3: " & m.Groups(4).Value)
    sb.AppendLine("#4: " & m.Groups(5).Value)
    sb.AppendLine("#5: " & m.Groups(6).Value)
    sb.AppendLine("Powerball: " & m.Groups(7).Value)
    sb.AppendLine()
Next
MessageBox.Show(sb.ToString)

Granted it is not the most elegent RegEx ever, but it should be easy enough to follow:

  • \\d stands for a single digit
  • / is the literal slash character
  • {4} is a quantifier, meaning 4 of the previous thing
  • /s is whitespace
  • /s+ means as much whitespace as you like
  • () denote capturing groups, allowing the access to the values in the .Groups property of each match

So you find all matches in a given text and can easily extract all subgroups by iterating over the matches.


For your general question:

Numbers(i) = Full_History.Substring(10, 16)                         'Store the numbers drawn
Balls(i) = Full_History.Substring(26, 14)   

These lines will always crop the same part out of the source string (eg index 10 to 26). You need to scale the Substring start part with the Start variable as well, like

Numbers(i) = Full_History.Substring(Start + 10, 16)                         'Store the numbers drawn
Balls(i) = Full_History.Substring(Start + 26, 14)   

Otherwise it is not suprising that each element ends up the same. Or to avoid more confusion, first crop the whole thing out and then dissect this new string into its parts:

For i As Integer = 0 To Array_Size - 1 Step 1                           'Loop through the string
    Dim ThisDraw As String = Full_History.Substring(Start, Finish)
    Full_Draw(i) = ThisDraw
    'Notice the fixed indizes now
    Dates(i) = ThisDraw.Substring(0, 10)                        'Store the Date of the drawing
    Numbers(i) = ThisDraw.Substring(10, 16)                         'Store the numbers drawn
    Balls(i) = ThisDraw.Substring(26, 14)                           'Store the 'ball' if necessary
    Start += Finish
Next

thank you for your responses. I fixed the issue by cutting out a single drawing in an array, and then cutting a substring (single Drawing) out of that, instead of trying to cut the Drawings, Dates, Numbers and Balls out of the "FullString" array straight from the stream.

Thanks for all the help Jim

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM