Hello,

I have a one-dimensional array that has one number per element. I also have a variable number, var. I need to take the number of elements from the first array that equals the var and put it into a second array with commas between the numbers. I would also like to have a title element in the second array.

This is how my first array looks:
50
216
459
56
2168
12
589
92

This is how I would like my new array to look: (var = 2)
Title
50,216
459,56
2168,12
589,92

This code I'm using right now to try and do this is:

Dim UnsortedNumArray() As String = Array.CreateInstance(GetType(String), Length - Var)
        Array.Copy(UnsortedArray, Var, UnsortedNumArray, 0, Length - Var)
        Dim NumArray As Array = Array.CreateInstance(GetType(String), UnsortedNumArray.Length)
        Dim SpaceArray As String = String.Join(" ", UnsortedNumArray)
        Dim ListNumArray() As String = SpaceArray.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
        Array.Copy(ListNumArray, 0, NumArray, 0, ListNumArray.Length)
        Dim ListArray As String = String.Join(",", ListNumArray)
        Dim ListVarArray() As String = ListArray.Split(",")

But it just gives me what I started with before that. I did try to use this code for awhile:

For x = 0 To UnsortedNumArray.Length
            If (UnsortedNumArray(x), " ").Length < Var Then
                Add(UnsortedNumArray(x + 1))
            Else
                (UnsortedNumArray(x), " ").Length >= Var
            End If
        Next

But It worked even less; it crashed the debug.

How would I go about doing this?

Can't you just output the new array to an arraylist? For instance...

dim newArray as new arraylist

newArray.add = "Title"
dim strTemp as string = ""
for i as integer = 0 to arrStuff.length - 1
   strTemp = ""
   for j = 0 to var - 1
      ' try catch block here...
      strTemp += arrStuff(i) & ","
      i += 1
      ' ...to here
   next j
   strTemp = strTemp.substring(0, strTemp.length - 1) 'to get rid of the extra ,
   newArray.add(strTemp)
next i

Or I suppose you can just create a large array of strings and redim preserve it later, depending on size.

You may also want to wrap the nested for loop in a try catch block in case your array isn't exactly divisible by your var number.

Not sure if I got the idea and it is untested but it should get you started. :)

-Josh

Sorry, line 3 should be:
newArray.add("Title")

Here we go! It works. :)

Dim newArray As New ArrayList
Dim arrStuff() As Integer = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Dim var As Integer = 2

newArray.Add("Title")

Dim strTemp As String = ""
For i As Integer = 0 To arrStuff.length - 1
    strTemp = ""
    For j = 0 To var - 1
        Try
            strTemp += arrStuff(i) & ","
            i += 1
        Catch ex As Exception
            Exit For
        End Try
    Next j
    i -= 1

    strTemp = strTemp.Substring(0, strTemp.Length - 1) 'to get rid of the extra ,
            newArray.Add(strTemp)
    Next i

That was it! Thank You so very very much! :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.