vb.net - Having trouble with an algorithm that loops through a string -
i using visual studio community.
hi, wrote loop loops through string of lottery drawings.i grab full drawing , cut out date, drawings , powerball numbers.
everything works first time through loop, , 06/29/2002 04 33 35 36 45 powerball: 22
, second time through loop 07/03/2002 " 04 33 35 36 45 " powerball: 22.
the second time through loop goes numbers first drawing. know write
numbers(i) = full_history.substring(start + 10, 16)
, , balls(i) = fullhistory.substring(start + 26, 14)
, doesn't seem right. hope explained right. appreciated.
dim start integer = 0 'create variable start dim finish integer = 40 integer = 0 array_size - 1 step 1 'loop through string full_draw(i) = full_history.substring(start, finish) 'store full drawing dates(i) = full_history.substring(start, 10) 'store date of drawing numbers(i) = full_history.substring(10, 16) 'store numbers drawn balls(i) = full_history.substring(26, 14) 'store 'ball' if necessary start += 40 'increment start variable
if have such specialized text can use regular expression extract data, in snippet:
dim rx 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 new system.text.stringbuilder each m 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 not elegent regex ever, should easy enough follow:
- \d stands single digit
- / literal slash character
- {4} quantifier, meaning 4 of previous thing
- /s whitespace
- /s+ means whitespace like
- () denote capturing groups, allowing access values in .groups property of each match
so find matches in given text , can extract subgroups iterating on matches.
for general question:
numbers(i) = full_history.substring(10, 16) 'store numbers drawn balls(i) = full_history.substring(26, 14)
these lines crop same part out of source string (e.g. index 10 26). need scale substring start part start
variable well, like
numbers(i) = full_history.substring(start + 10, 16) 'store numbers drawn balls(i) = full_history.substring(start + 26, 14)
otherwise not suprising each element ends same. or avoid more confusion, first crop whole thing out , dissect new string parts:
for integer = 0 array_size - 1 step 1 'loop through string dim thisdraw string = full_history.substring(start, finish) full_draw(i) = thisdraw 'notice fixed indizes dates(i) = thisdraw.substring(0, 10) 'store date of drawing numbers(i) = thisdraw.substring(10, 16) 'store numbers drawn balls(i) = thisdraw.substring(26, 14) 'store 'ball' if necessary start += finish next
Comments
Post a Comment