Solved

# Number Parsing Help....Works, just need a second set of eyes to see a simple error im sure

Posted on 2005-03-20
Medium Priority
182 Views
ok, the problem is this, i get a lot of good results, and then this occasionally happens

------------------------*****************-----------------------
11096114-11097444:11097446-11098989:11098991-11124273:1112427-1112427:4-4:11124275-11131129

under the ***** those numbers should have been included, making those 3 complete sets just one
my code follows

Dim numbers As String
Dim nlow, nhigh As String
nlow = Int(CInt(nums.Item(0).ToString))
nhigh = Int(CInt(nums.Item(0).ToString))
For x As Integer = 1 To nums.Count - 1
If x < nums.Count - 1 Then
If Int(nhigh) + 1 = Int(CInt(nums.Item(x).ToString)) Then
nhigh = CInt(nums.Item(x).ToString)
Else
numbers = numbers & ":" & nlow & "-" & nhigh
nhigh = Int((CInt(nums.Item(x)).ToString))
nlow = nhigh
End If
End If
Next

0
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions

LVL 12

Expert Comment

ID: 13584953
What are you trying to achieve exactly?  I mean, what data value/s are you trying to extract from the string?
0

LVL 4

Expert Comment

ID: 13585048
I assume you're trying to get the two numbers, of which the first one before the dash is the lower value, and then the second being the higher value.

Maybe try using .Split() on the input string using the colon as the first delimiter, and then for each string in the resulting array similarly splitting on the dash?

I don't know my regexp, so that's my suggestion.
0

LVL 4

Expert Comment

ID: 13585157
OK, I've come back with something to mess around with.  I'm still hoping that someone with a better idea of regular expressions (and how to use them!) will correct me, or provide a better solution  :o)

Anyway, here's a very quick and dirty regular expression to play around with.  Hope it makes sense

===  CODE  ===

Dim ts As String = "11096114-11097444:11097446-11098989:11098991-11124273:1112427-1112427:4-4:11124275-11131129"
Dim regex As String = "(?<low>\d+)-(?<high>\d+)[:]*"
Dim options As RegexOptions = ((RegexOptions.IgnorePatternWhitespace Or RegexOptions.Multiline) _
Or RegexOptions.IgnoreCase Or RegexOptions.ExplicitCapture)
Dim reg As Regex = New Regex(regex, options)
Dim mc As MatchCollection
Dim ll, hh As Integer

Dim mc As MatchCollection
mc = reg.Matches(ts)
For Each m As Match In mc
ll = Integer.Parse(m.Groups("low").Value)
hh = Integer.Parse(m.Groups("high").Value)
Next

===  CODE   ===
0

LVL 1

Author Comment

ID: 13586670
oh ya, those numbers at the top are results...i probably should have been more clear about that.....the numbers that are used to find those results are.....

1
2
3
4
5
7
8
9
10
11
12
14
15
18
(but only a lot larger....)
0

LVL 2

Expert Comment

ID: 13587798
use Long instead of Integer (and CLng instead of CInt)
0

LVL 1

Author Comment

ID: 13588224
ok, well, i tried using long instead of integer, and clng instead of cint, i may not have done it right, but it doesnt work anymore....

Dim numbers As String
Dim nlow, nhigh As Long
nlow = CLng(nums.Item(0))
nhigh = CLng(nums.Item(0))
For x As Integer = 1 To nums.Count - 1
If x < nums.Count - 1 Then
If nhigh + 1 = CLng(nums.Item(x)) Then
nhigh = CLng(nums.Item(x))
Else
If nhigh < nlow Then
numbers = numbers & ":" & nlow & "-" & nhigh
nhigh = CLng(nums.Item(x))
nlow = nhigh
End If
End If
End If
Next
0

LVL 1

Author Comment

ID: 13588241
ok, What I am trying to achieve exactly is to limit the amount of entries in my database....
the numbers I am parsing can run for 10000 striaght without skipping one digit, so its pointless to have all those numbers,
i can just make a low, high range....back to my reference up top....

------------------------*****************-----------------------
11096114-11097444:11097446-11098989:11098991-11124273:1112427-1112427:4-4:11124275-11131129

these are sample results using the code from my first post on this topic, the problem I am having is, where you see the *****
those three sets of numbers should just be one set

11098991-11131129

something happens in my loop where it messes up.....**********
when you look at the numbers more closely, you see that the numbers under the **** before its parsed is actually 11124274......
0

LVL 1

Author Comment

ID: 13588300
more sample results....

:12863937-12864804:12864-12864:805-805:12864806-12865534:12865-12865:535-535:12865536-12866118
:12866-12866:119-119:12866120-12867140:12867-12867:141-141:12867142-12868308:12868-12868:309-309
:12868310-12868454:12868-12868:455-455:12868456-12868600:12868-12868:601-601:12868602-12870060
:12870-12870:61-61:12870062-12872419
0

LVL 1

Author Comment

ID: 13588303
and I will post my code one more time just incase its changed.....

Public Sub finished()
Dim numbers As String
Dim nlow, nhigh As String
nlow = CInt(nums.Item(0).ToString)
nhigh = CInt(nums.Item(0).ToString)
For x As Integer = 1 To nums.Count
If x < nums.Count - 1 Then
If nhigh + 1 = CInt(nums.Item(x).ToString) Then
nhigh = CInt(nums.Item(x).ToString)
Else
'   If nhigh < nlow Then
numbers = numbers & ":" & nlow & "-" & nhigh
nhigh = CInt(nums.Item(x))
nlow = nhigh
'End If
End If
End If
Next
numbers = numbers & ":" & nlow & "-" & nhigh
RaiseEvent newartnum(numbers, group, low, high, lastnum)
End Sub
0

LVL 1

Author Comment

ID: 13588307
ok i was just going thru all those sample results....and it looks like they are all consecutive....none missing....any ideas?
0

LVL 1

Author Comment

ID: 13588312
more sample results where numbers are omitted

:10228430-10252833:10252835-10262658:1026265-1026265:9-9:10262660-10286310:1028631-1028631:1-1:10286312-10293881
0

LVL 4

Expert Comment

ID: 13588669
Just from what I can read, I can't see anything blindingly obviously wrong with the code you have there.

Have you checked the incoming data to make sure that it's all ok?
0

LVL 1

Author Comment

ID: 13589000
i was just messing around with this code and similar.....maybe it could spark some ideas....

If nlow < low And CInt(nums.Item(x + 1)) < low Then
x += 1
nlow = nlow.ToString & nums.Item(x).ToString
End If

.....I believe it is the incoming data that is messed up, all the data is stored in nums (ArrayList)
the smallest list I have been working with is a list of  8400+ numbers....its a bit much to scan thru, but I did the following....
(low is a public declare stating the lowest number, there is also a high declare stating the highest possible)

If nlow < low Then
RaiseEvent onap("Whoa: " & nlow & ":" & nums.Item(x) & ":" & nums.Item(x + 1) & vbCrLf)
End If

i recieved the following results

Whoa: 12:12Whoa: 865102:865102:12865103

Whoa: 12:12Whoa: 865248:865248:12865249

Whoa: 12:12Whoa: 865394:865394:12865395

Whoa: 12:12Whoa: 865686:865686:12865687

Whoa: 12:12Whoa: 866270:866270:12866271

Whoa: 12:12Whoa: 866416:866416:12866417

Whoa: 12:12Whoa: 866562:866562:12866563

Whoa: 12:12Whoa: 867584:867584:12867585

Whoa: 12:12Whoa: 867730:867730:12867731

Whoa: 12:12Whoa: 869044:869044:12869045

Whoa: 12:12Whoa: 869190:869190:12869191

0

LVL 1

Author Comment

ID: 13589011
erm
those results arent that great....these are more common.....

Whoa: 5:5Whoa: 71603:071603:5071604

Whoa: 507273:507273Whoa: 8:8:5072739

Whoa: 5074:5074Whoa: 36:036:5074037

Whoa: 507565:507565Whoa: 8:8:5075659

Whoa: 5076:5076Whoa: 956:956:5076957

Whoa: 507711:507711Whoa: 8:8:5077119

Whoa: 5:5Whoa: 77443:077443:5077444

Whoa: 507:507Whoa: 9065:9065:5079066

Whoa: 5081:5081Whoa: 336:336:5081337

Whoa: 5087:5087Whoa: 177:177:5087178

Whoa: 50894:50894Whoa: 49:49:5089450
0

LVL 4

Expert Comment

ID: 13589182
I'm a little puzzled with this:

nlow = nlow.ToString & nums.Item(x).ToString

I presume nlow is still a number type of some sort, say an integer?  If so, then is there any reason why you're concatenating two strings and doing an implicit Integer.Parse on the resultant string?
0

LVL 4

Expert Comment

ID: 13589373
Oh, one other thing that I forgot to mention before.

You use this a lot:

CInt(nums.Item(x).ToString)

Personally, I think that it makes the code more confusing to read, compared to this:

CInt(nums.Item(x))

Or this:

CInt(nums(x))

This last form is valid in C# or VB.NET, since it uses the default indexer on the ArrayList.
0

LVL 1

Author Comment

ID: 13592795
ok the reason i may want to combine two numbers together, is cause, thats what the problem is, somehow the "problem numbers" are getting broken up into two pieces......
0

LVL 4

Expert Comment

ID: 13593874
I'm sure I'm missing something crucial in understanding what's wrong with the data, but it seems like all the results are ok in this last batch you've provided.

Any possibility of providing some data where the resultant number is NOT what you expect?

I'm just trying to backtrack the path of the data, to get a better understanding of what's happening to it, and where.

(It's just gone past 2:30am here :o)
0

LVL 1

Author Comment

ID: 13597594
Sample data
I logged this data.
This data is sent one number per line, or supposed to be...so it iadded like so...

Public Sub newnum(ByVal text As String)
End Sub

nums being an arraylist

28655860
28655861
28655862
28655
863
28655864
28655865
28655866
28671046
28671047
2867104
8
28671049
28671050
28671051

i somehow need to figure out how to reassemble those numbers that come in messed up....
0

LVL 1

Author Comment

ID: 13597622
results on that should be

:28655860-28655866:28671046-18671051

0

LVL 4

Accepted Solution

wyoung76 earned 2000 total points
ID: 13598275
I have no idea if this'll go any way to helping you further, but using this data:

28655860
28655861
28655862
28655
863
28655864
28655865
28655866
28671046
28671047
2867104
8
28671049
28671050
28671051

And this code:

Public Sub finished()
Dim numbers As String
Dim nlow, nhigh As Integer
nlow = nums(0)
nhigh = nums(0)
For x As Integer = 1 To nums.Count
If x < nums.Count Then
If CInt(nums(x)) < nlow Then
If CInt(nums(x + 1)) < nlow Then
nums(x + 1) = CInt(nums(x)) & CInt(nums(x + 1))
nums(x) = 0
x += 1
End If
End If

If nhigh + 1 = CInt(nums(x)) Then
nhigh = CInt(nums(x))
Else
'   If nhigh < nlow Then
numbers = numbers & ":" & nlow & "-" & nhigh
nhigh = CInt(nums(x))
nlow = nhigh
'End If
End If
End If
Next
numbers = numbers & ":" & nlow & "-" & nhigh
RaiseEvent newartnum(numbers, group, low, high, lastnum)
End Sub

I get this type of result:

:28655860-28655866:28671046-28671051

Am I still getting the wrong end of the stick?  :o)
0

LVL 1

Author Comment

ID: 13598430
what did you use to input the data into nums?
0

LVL 4

Expert Comment

ID: 13598509
I just mocked up an ArrayList inside the function like so:

Dim nums As New ArrayList
nums.AddRange(New Integer() {28655860, 28655861, 28655862, 28655, 863, 28655864, 28655865, 28655866, 28671046, 28671047, 2867104, 8, 28671049, 28671050, 28671051})
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ruâ€¦
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itselâ€¦
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized â€¦
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: â€¦
###### Suggested Courses
Course of the Month10 days, 23 hours left to enroll

#### 770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.