casa
casa

Reputation: 61

VB.NET Creating groups from elements of different arrays

I have these arrays with different lengths and elements. The number of these arrays will be different every time but it will be always more than one array.

Now, I have to create groups from every element of these arrays combined to the elements of the other array and save them on a database. In short, 1 group should be composed of 1 element from every array.

The product of every array length should be the total number of groups to be generated.

It should look like this:

    1ST ARR - [a1, a2, a3, a4]
    2ND ARR - [b1, b2, b3]
    3RD ARR - [c1, c2, c3]
    4TH ARR - [d1, d2, d3, d4, d5]

    1ST GROUP : a1|b1|c1|d1 (I just used "|" as separator)
    2ND GROUP : a2|b1|c1|d1
    3RD GROUP : a3|b1|c1|d1
    4TH GROUP : a4|b1|c1|d1
    5TH GROUP : a1|b2|c1|d1 (index of 2nd array is now incremented)
    6TH GROUP : a2|b2|c1|d1
    7TH GROUP : a3|b2|c1|d1
    8TH GROUP : a4|b2|c1|d1

and so on...

I tried to put my arrays on a dictionary and looped through it with integer counters and I have successfully looped through the elements of the first array. But when it comes to the second array, what happens from my code it the the first array and second array happens to be incremented at the same time but from how I understood it, the code should loop through the first array before increasing the value for the index of the second array.

From my sample, this is what happens on my code:

    1ST GROUP : a1|b1|c1|d1 (ok)
    2ND GROUP : a2|b1|c1|d1 (ok)
    3RD GROUP : a3|b1|c1|d1 (ok)
    4TH GROUP : a4|b1|c1|d1 (ok)
    5TH GROUP : a1|b2|c1|d1 (ok)
    6TH GROUP : a2|b3|c1|d1 (should be b2)

This is a sample code on how I did it:

    Dim ls_arr1() As String = {"a1", "a2", "a3", "a4"}
    Dim ls_arr2() As String = {"b1", "b2", "b3"}
    Dim ls_arr3() As String = {"c1", "c2", "c3"}
    Dim ls_arr4() As String = {"d1", "d2", "d3", "d4", "d5"}
    Dim l_categories As New Dictionary(Of String, String())
    Dim ls_currArr() As String = Nothing
    Dim ls_grouped As String = ""

    Dim li_grpCount As Integer = 1
    Dim n As Integer = 0

    Dim lb_Ctr() As Boolean = Nothing
    Dim l_indices As New Dictionary(Of String, Integer)

    l_categories.Add("ARR1", ls_arr1)
    l_categories.Add("ARR2", ls_arr2)
    l_categories.Add("ARR3", ls_arr3)
    l_categories.Add("ARR4", ls_arr4)


    For Each pair As KeyValuePair(Of String, String()) In l_categories
        'COUNT NUMBER OF GROUPS THAT MUST BE GENERATED
        ls_currArr = pair.Value.ToArray
        li_grpCount = li_grpCount * ls_currArr.Count

        'THIS WILL BE THE INDEX FOR EVERY ARRAY
        l_indices.Add(pair.Key, 0)
    Next

    For x As Integer = 1 To li_grpCount
        For i As Integer = 0 To l_indices.Count - 1
            Dim l_pairKey As String = l_indices.ElementAt(i).Key
            Dim l_pairVal As Integer = l_indices.ElementAt(i).Value

            ls_currArr = l_categories.Item(l_pairKey).ToArray
            ls_grouped &= ls_currArr(l_pairVal)
            If i < l_indices.Count - 1 Then
                ls_grouped &= "|"
            End If

            'I USED lb_Ctr TO KNOW IF THE INDEX SHOULD BE INCREMENTED
            If lb_Ctr(n) = True Then
                If (l_indices.Item(l_pairKey) + 1) < l_categories.Count Then
                    l_indices.Item(l_pairKey) += 1
                Else
                    l_indices.Item(l_pairKey) = 0
                    lb_Ctr(n + 1) = True
                End If
            End If
            n += 1
        Next

        'MY SAVE TO DATABASE CODE

        ls_grouped = ""
        n = 0
    Next

Any help will be appreciated. Even if it is by means of not using Dictionary, it's okay. Thank you so much.

Upvotes: 0

Views: 717

Answers (2)

user11982798
user11982798

Reputation: 1908

    Dim myList As New List(Of Array)


    myList.Add({"a", "B", "d"})
    myList.Add({"a", "B", "d"})
    myList.Add({"a", "B", "d"})

    Dim myPos(myList.Count) As Integer

    Dim myTotalCnt As Integer = 1
    For myCnt = 1 To myList.Count
        myTotalCnt = myTotalCnt * myList(myCnt - 1).Length
    Next

    Dim myListRslt As New List(Of String)

    Dim myIncr As Integer = 0
    For myCnt = 1 To myTotalCnt
        Dim myStrList As New List(Of String)
        For myCnt1 = 1 To myList.Count
            myStrList.Add(myList(myCnt1 - 1)(myPos(myCnt1 - 1)))
        Next

        Dim roro = String.Join("|", myStrList)
        myListRslt.Add(roro)

        myIncr = 1

        For myCnt2 = 1 To myList.Count
            If myPos(myCnt2 - 1) + 1 < myList(myCnt2 - 1).Length Then
                If myIncr = 1 Then
                    myPos(myCnt2 - 1) += 1
                    myIncr = 0
                End If
            Else
                myPos(myCnt2 - 1) = 0
                myIncr = 1
            End If
            Debug.Print(myPos(myCnt2 - 1))
        Next
    Next

Upvotes: 0

user11982798
user11982798

Reputation: 1908

    Dim ls_arr1() As String = {"a1", "a2", "a3", "a4"}
    Dim ls_arr2() As String = {"b1", "b2", "b3"}
    Dim ls_arr3() As String = {"c1", "c2", "c3"}
    Dim ls_arr4() As String = {"d1", "d2", "d3", "d4", "d5"}
    Dim myList As New List(Of String)

    For Each myArr4 In ls_arr4
        For Each myArr3 In ls_arr3
            For Each myArr2 In ls_arr2
                For Each myArr1 In ls_arr1
                    myList.Add(myArr1 & "|" & myArr2 & "|" & myArr3 & "|" & myArr4)
                Next
            Next
        Next
    Next

Upvotes: 0

Related Questions