|
-
Aug 28th, 2000, 08:23 AM
#1
Thread Starter
New Member
Hi:
I'm new to VB (about a week). I'm writing something in excel to sort some reference designators for circuit boards. What I need to do is get this:
C1, C8, C2, C6, C12, C3
to this:
C1-3, C6, C8, C12
I'm pretty close except for one last thing that's driving me mad. I have all the numbers stored in an integer array like this:
1,2,3,6,8,12
I need to compress them to an array of strings like this:
1-3
6
8
12
After that I can stick the C's back on no problem. All the arrays have to be dynamically sized of course. This program would save people time here, cause doing this by hand really sucks!
more examples in case that wasn't clear:
1,5,7,8,9,10,23 ---> 1,5,7-10,23
1,2,3,4,23,67,68,69 ---> 1-4,23,67-69
I've worked on this for a awhile and anything I come up with gets really complex quickly. There has to be an elegant way to do this, I'm just missing it somehow.
any ideas?
thanks,
edchainsaw
-
Aug 28th, 2000, 11:30 AM
#2
I think this works, but I only tested a few cases.
Code:
Dim intMyNumbers(6) As Integer
Dim strMyStrings() As String
Dim intNumberNdx As Integer
Dim intStringNdx As Integer
Dim bHasdash As Boolean
intMyNumbers(0) = 1
intMyNumbers(1) = 5
intMyNumbers(2) = 7
intMyNumbers(3) = 8
intMyNumbers(4) = 9
intMyNumbers(5) = 10
intMyNumbers(6) = 23
ReDim strMyStrings(0)
strMyStrings(0) = intMyNumbers(0)
For intNumberNdx = 1 To UBound(intMyNumbers)
If intMyNumbers(intNumberNdx) = intMyNumbers(intNumberNdx - 1) + 1 Then
If Not bHasdash Then
strMyStrings(intStringNdx) = strMyStrings(intStringNdx) & "-"
bHasdash = True
End If
' Find the position of the dash, and recreate the array entry by
' concatenating the left side including the dash and the new number
strMyStrings(intStringNdx) = Left$(strMyStrings(intStringNdx), InStr(1, strMyStrings(intStringNdx), "-")) & intMyNumbers(intNumberNdx)
Else
bHasdash = False
intStringNdx = intStringNdx + 1
ReDim Preserve strMyStrings(intStringNdx)
strMyStrings(intStringNdx) = intMyNumbers(intNumberNdx)
End If
Next
For intStringNdx = 0 To UBound(strMyStrings)
Debug.Print strMyStrings(intStringNdx)
Next
-
Aug 28th, 2000, 12:30 PM
#3
Thread Starter
New Member
Thanks!
Thanks MartinLiss! It works great... My array started with
element one, but once I fixed that everything was all good.
I guess vb arrays should start with 0 just like in c. I
don't know why I thought otherwise. What I was working on
was similar, there just got to be too many flags and
variables confusing the issue.
I really liked the way you replaced the number after the
dash each time. I didn't think of that...
Lots of poor data entry people thank you!
Ed
-
Aug 28th, 2000, 12:45 PM
#4
You're welcome. BTW, it's unfortunate, but VB isn't consistent as far as being zero- or one-based. For example while arrays, forms and object collections are zero-based, a collection object itself is one-based. If you want arrays to start with 1 you can add an Option Base 1 statement in the module or form but that is rarely (if ever) done.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|