Resolved Combine array of numbers into dashed number (1,2,3,4) becomes (1-4)

Fedaykin

Active member
Joined
Mar 20, 2013
Messages
30
Programming Experience
Beginner
Does anyone know of a method to combine 1,2,3,4 into 1-4? I know normally people ask the opposite, but I need this for a specific machine that wants the numbers grouped together. My actual issue will be more like C1, C2, C3, C4 becomes C1-C4.

I've looked at Join and Split, but these don't seem to apply. I will be reading this from a DataTable.
 
Last edited:
Interesting question, not really sure how I would handle it, but it would definitely include lots of code I would want to get rid of in the end. How else are you grouping your data? Some example data and result would be needed to really get a handle on it.
 
The data will be read in from an Excel file into a DataTable

Ultimately the datatable would include these values:

C1, C2, C3, C4, C10, C12, C13, C14, C15

The result should be:

C1-C4, C10, C12-C15


For proof of concept I was just going to start with the numerals and get after the alpha characters at a later date.

So:

1, 2, 3, 4, 10, 12, 13, 14, 15

Would yield:

1-4, 10, 12-15
 
A rough VB translation of something I found when searching "group consecutive numbers":
        Dim numbers = {1, 2, 3, 4, 10, 12, 13, 14, 15}
        Dim first As Integer, groups As New List(Of String)

        For i = 0 To numbers.Length - 1
            If i + 1 = numbers.Length OrElse numbers(i) + 1 < numbers(i + 1) Then
                If first = i Then
                    groups.Add(numbers(i).ToString)
                Else
                    groups.Add(String.Format("{0}-{1}", numbers(first), numbers(i)))
                End If
                first = i + 1
            End If
        Next

        Dim display = String.Join(", ", groups.ToArray)
 
That looks promising, I saw that one earlier.

I'm in the middle of trying to translate another one i found when searching "Hyphenate consecutive numbers in array". I will post it when it is less ugly, and then I will try yours as well.
 
Wow, that is a lot more elegant than what i was trying. I tweaked it to sort the array before smashing it down. Your solution works great:

VB.NET:
        Dim numbers = {1, 2, 15, 4, 10, 12, 13, 14, 3}
        Array.Sort(numbers)
        Dim first As Integer, groups As New List(Of String)


        For i = 0 To numbers.Length - 1
            If i + 1 = numbers.Length OrElse numbers(i) + 1 < numbers(i + 1) Then
                If first = i Then
                    groups.Add(numbers(i).ToString)
                Else
                    groups.Add(String.Format("{0}-{1}", numbers(first), numbers(i)))
                End If
                first = i + 1
            End If
        Next


        Dim display = String.Join(", ", groups.ToArray)


        MsgBox(display)



I was trying something much more complicated (hacked from another site) but I didn't quite get it to advance to the next number in the array. This does NOT work as it's written, but I was trying it just to learn:

VB.NET:
        Dim VString As String
        Dim a() As Integer = {1, 101, 6, 34, 7, 4, 5, 39, 8, 2002, 2003, 2004}
        Array.Sort(a)
        Dim x As Integer = 1


        VString = a(x)


        While (True)


            If x + 2 <= a(0) Then


                If a(x + 1) = a(x) + 1 And a(x + 2) = a(x) + 2 Then
                    VString = VString & " - "
                    x = x + 2
                    While x < a(0)
                        If a(x + 1) = a(x) + 1 Then
                            x = x + 1
                        Else
                            MsgBox("Mid End.")
                        End If
                    End While
                    VString = VString & a(x)
                End If
            End If


            If x + 1 <= a(0) Then
                x = x + 1
                VString = VString & ", " & a(x)
            End If


            If x = a(0) Then
                MsgBox("End End.")
            End If


        End While
        MsgBox(VString)
 
Back
Top