none
Very Basic Programming Question about Nested Loops

    Question

  • Hello, some days I think I am decent programmer. Today is not one of those days. I know there is an elegant solution to this problem, but I cannot see the endgame. Does anyone have any tips for making this snippet more elegant? It's a very basic example, but one that I think is common programming challenge. If I have unknown number of lists of string variables of unknown length (It is unknown to me until runtime), how can I use just one loop or a loop plus a nested loop to create a matrice of the values?

    Easy to re-create. Create a VB.NET Form application, drag over two richtext boxes and a button, then paste this into the form class

    Public Class Form1
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        Dim List As String()
    
        List = RichTextBox1.Text.Split("|")
    
        Select Case List.Length
    
          Case 1
    
            For Each ListValue As String In List
    
              Dim List1 As String()
    
              List1 = ListValue.Split(";")
    
              For Each List1Value In List1
                'Show my values
                RichTextBox2.Text = RichTextBox2.Text & List1Value.ToString() & ";"
              Next
    
            Next
    
          Case 2
    
            For Each ListValue As String In List
    
              Dim List1 As String()
    
              List1 = ListValue.Split(";")
    
              For Each List1Value In List1
    
                Dim List2 As String()
    
                List2 = List(1).ToString.Split(";")
    
                For Each List2Value In List2
    
                  RichTextBox2.Text = RichTextBox2.Text & List1Value.ToString() & List2Value.ToString() & ";"
    
                Next
    
                RichTextBox2.Text = RichTextBox2.Text & "|"
    
              Next
    
              'Stop after the first iteration - "Cascading"
    
              Exit For
            Next
    
          Case 3
            'and so on.
          Case 4
    
    
            'How can I do this without defining a Case for every additional list?
            'How can I loop once without knowing the bounds of my matrices?
            'There must be a more elegant solution!!!
    
        End Select
    
      End Sub
    
    End Class
    
    Wednesday, June 23, 2010 10:34 PM

Answers

  • Hi again Jordan B,

    Here is your above 2 example lists merged in a Function I have called MergeListItems

    I hope it helps you to merge any two lists of type STRING together. :-)

     

    Add one Button to a Form and try this code please.>>

     

    Public Class Form1
    
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
      Dim list1 As New List(Of String)
      list1 = (New String() {"1", "2", "3"}).ToList
    
      Dim list2 As New List(Of String)
      list2 = (New String() {"2", "4", "6"}).ToList
    
      Dim mergedList As New List(Of String)
      mergedList = MergeListItems(list1, list2)
    
      Dim sb As New System.Text.StringBuilder
      For index As Integer = 0 To mergedList.Count - 1
       sb.Append(mergedList.Item(index) & ",")
      Next
    
      MessageBox.Show(sb.ToString)
    
     End Sub
    
     Private Function MergeListItems(ByVal lst1 As List(Of String), ByVal lst2 As List(Of String)) As List(Of String)
    
      Dim returnedList As New List(Of String)
      If lst1.Count <> lst2.Count Then
       Return returnedList
      Else
       For index1 As Integer = 0 To lst1.Count - 1
        For index2 As Integer = 0 To lst2.Count - 1
         returnedList.Add(lst1.Item(index1) & lst2.Item(index2))
        Next
       Next
      End If
    
      Return returnedList
    
     End Function
    
    End Class

     


    Regards, John : Trying hard for another goal into the .Net!!
    Thursday, June 24, 2010 4:13 PM
  • During the recursion use a level variable to identify the dimension, and even you can use it for getting the separator at different levels
    Muthu Krishnan.R Use only what you need, Reduce global warming
    Thursday, June 24, 2010 9:13 PM

All replies

  • Are you familiar with recursion? That might be the right technique for this case.

    http://en.wikipedia.org/wiki/Recursion

    http://visualbasic.about.com/od/quicktips/qt/recursion.htm

    Here are the steps:

    1) Put your for/each code into a subroutine that is called from the click event.

    2) When you perform the second split, determine if the resulting list has a value. If so, call the subroutine again.

    This should allow you to call the routine any number of times for as many "levels" down that you need.

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Wednesday, June 23, 2010 10:46 PM
  • Hi Jordan B,

    Are you starting with one long string and you wish to split it into separate lists?

    Is each line in the original string got different separator characters characters though like

    | ; , :

    ?

     


    Regards, John : Trying hard for another goal into the .Net!!
    Thursday, June 24, 2010 12:23 AM
  • Actually, I'm starting with one long string and wishing to match every value of each list to every value of each list, like a matrice.

    so

    1;2;3|2;4;6

    should become

    12;14;16|22;23;26|32;34;36

    but not

    12;14;16|22;23;26|32;34;36|21;22;23|41;42;43|61;62;63

    the values should not be matched in reverse so it's not exactly a true matrice, but one that is restricted by each parent list of values (like a cascading list of paremeters)

    That's why I stop the iteration in Case 2 after the first list has completed it's loop

     

    Thursday, June 24, 2010 1:37 PM
  • Thanks for your reply.

    I believe I know how to implement recursion, a very basic example would be a factorial

     

    	Public Function Factorial(ByVal n as integer) as Double
    		If n= 0 Then
    			Factorial = 1
    		Else
    			Factorial = Factorial (n-1) * n
    		End If
    	End Function

     I will give that a try and let you know how it works thanks!

    Thursday, June 24, 2010 1:52 PM
  • Actually, I'm starting with one long string and wishing to match every value of each list to every value of each list, like a matrice.

    so

    1;2;3|2;4;6

    should become

    12;14;16|22;23;26|32;34;36

    but not

    12;14;16|22;23;26|32;34;36|21;22;23|41;42;43|61;62;63

    the values should not be matched in reverse so it's not exactly a true matrice, but one that is restricted by each parent list of values (like a cascading list of paremeters)

    That's why I stop the iteration in Case 2 after the first list has completed it's loop

     

    Hi again Jordan B,

    Should that not be.>>

    1;2;3|2;4;6

    should become

    12;14;16|22;24;26|32;34;36

    ?


    Regards, John : Trying hard for another goal into the .Net!!
    Thursday, June 24, 2010 3:09 PM
  • Hi again Jordan B,

    Here is your above 2 example lists merged in a Function I have called MergeListItems

    I hope it helps you to merge any two lists of type STRING together. :-)

     

    Add one Button to a Form and try this code please.>>

     

    Public Class Form1
    
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
      Dim list1 As New List(Of String)
      list1 = (New String() {"1", "2", "3"}).ToList
    
      Dim list2 As New List(Of String)
      list2 = (New String() {"2", "4", "6"}).ToList
    
      Dim mergedList As New List(Of String)
      mergedList = MergeListItems(list1, list2)
    
      Dim sb As New System.Text.StringBuilder
      For index As Integer = 0 To mergedList.Count - 1
       sb.Append(mergedList.Item(index) & ",")
      Next
    
      MessageBox.Show(sb.ToString)
    
     End Sub
    
     Private Function MergeListItems(ByVal lst1 As List(Of String), ByVal lst2 As List(Of String)) As List(Of String)
    
      Dim returnedList As New List(Of String)
      If lst1.Count <> lst2.Count Then
       Return returnedList
      Else
       For index1 As Integer = 0 To lst1.Count - 1
        For index2 As Integer = 0 To lst2.Count - 1
         returnedList.Add(lst1.Item(index1) & lst2.Item(index2))
        Next
       Next
      End If
    
      Return returnedList
    
     End Function
    
    End Class

     


    Regards, John : Trying hard for another goal into the .Net!!
    Thursday, June 24, 2010 4:13 PM
  • During the recursion use a level variable to identify the dimension, and even you can use it for getting the separator at different levels
    Muthu Krishnan.R Use only what you need, Reduce global warming
    Thursday, June 24, 2010 9:13 PM
  • Hi,

     

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions? 

     

    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, June 29, 2010 1:24 AM
    Moderator