none
Match a strings RRS feed

  • Question

  • Hi All,

    Test to find a list A_ in list B_.

    The another way succed with Leshay code but I want try for this way.

    Somebody can fix for this code.

    Option Strict On
    Option Explicit On
    
    Imports System.IO
    Imports System.Data
    Imports System.Text
    
    Public Class Form1
    
        Dim A_ As New List(Of String)
        Dim B_ As New List(Of String)
        Dim C_ As New List(Of String)
    
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            A_.Add("768")
            A_.Add("836")
    
            For I As Integer = 0 To A_.Count - 1
                TextBox1.AppendText(A_(I) & vbCrLf)
            Next
    
            B_.Add("613,613,633,133,305,300,350,050,654,653,643,543,161,166,116,616,392,393,323,923,834,835,845,345,201,542,")
            B_.Add("635,638,658,358,142,142,122,422,694,696,646,946,429,427,497,297,824,822,078,242,543,547,201,437,386,411,")
    
            For I As Integer = 0 To B_.Count - 1
                TextBox2.AppendText(B_(I) & vbCrLf)
            Next
    
    
        End Sub
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            Dim sb As New StringBuilder
    
            For I As Integer = 0 To A_.Count - 1
    
                Dim StringToCheck As String = A_(I).ToString
                Dim StringsToCheck As String() = StringToCheck.Split(","c)
    
                For Each S As String In StringsToCheck
    
                    '0 1 2
                    '0 2 1
                    '1 2 0
                    '1 0 2
                    '2 0 1
                    '2 1 0
    
                    Dim StringsToFind As String() = {S(0) & S(1) & S(2),
                                                     S(0) & S(2) & S(1),
                                                     S(1) & S(2) & S(0),
                                                     S(1) & S(0) & S(2),
                                                     S(2) & S(0) & S(1),
                                                     S(2) & S(1) & S(0)}
    
                    For J As Integer = 0 To StringsToFind.Count - 1
                        sb.Append(StringsToFind(J) & vbCrLf)
                        C_.Add(StringsToFind(J))
                    Next
                Next
            Next
            Me.TextBox3.AppendText(sb.ToString)
    
    
            For I As Integer = 0 To B_.Count - 1
                Dim StrToCheck() As String = B_(I).Split(","c)
                Dim Count As Integer = 0
                For j As Integer = 0 To C_.Count - 1
                    For Each Check As String In StrToCheck
                        If Check.Contains(C_(j)) Then Count += 1
                    Next
                Next
                TextBox4.AppendText("Occurances (" & A_(I) & ")= " & Count.ToString & vbCrLf)
            Next
    
        End Sub
    End Class
    

    The error occur when we put the 678 in line2 in list B_

    Sunday, March 8, 2020 10:17 AM

Answers

All replies


  • The error occur when we put the 678 in line2 in list B_

    The problem isn't with what or where you add a number to the 2nd list.
    It's in your processing of the 1st list. If you change this:

    A_.Add("768")
    A_.Add("836")

    to this:

    A_.Add("836")
    A_.Add("768")

    you will find that you show 0 occurrences for 836 and 3 occurrences for 768.

    - Wayne

    Monday, March 9, 2020 3:51 AM
  •         B_.Add("613,613,633,133,305,300,350,050,654,653,643,543,161,166,116,616,392,393,323,923,834,835,845,345,201,542,")
            B_.Add("635,638,658,358,142,142,122,422,694,696,646,946,429,427,497,297,824,822,078,242,543,547,201,437,386,411,")
    
    

    For another hint at what's wrong with your logic, reverse the order of the 
    strings in the second list and see what happens to the output:

    B_.Add("635,638,658,358,142,142,122,422,694,696,646,946,429,427,497,297,824,822,078,242,543,547,201,437,386,678,")
    B_.Add("613,613,633,133,305,300,350,050,654,653,643,543,161,166,116,616,392,393,323,923,834,835,845,345,201,542,")
    

    - Wayne

    Monday, March 9, 2020 4:21 AM
  •         For I As Integer = 0 To B_.Count - 1
                Dim StrToCheck() As String = B_(I).Split(","c)
                Dim Count As Integer = 0
                For j As Integer = 0 To C_.Count - 1
                    For Each Check As String In StrToCheck
                        If Check.Contains(C_(j)) Then Count += 1
                    Next
                Next
                TextBox4.AppendText("Occurances (" & A_(I) & ")= " & Count.ToString & vbCrLf)
            Next
    
    

    What happens to your output if you change the above code to this?

    Suggested code deleted by poster as not viable. Pardon the distraction.

    Note that in the above quoted code you are using index I with A_ within a loop
    where variable I is controlled by the number of strings in B_ ...

    - Wayne



    • Edited by WayneAKing Monday, March 9, 2020 5:02 AM
    Monday, March 9, 2020 4:40 AM
  • Please note the editing to my prior post where I have removed the code suggestion
    and added a comment instead.

    - Wayne
    Monday, March 9, 2020 5:04 AM
  •         For I As Integer = 0 To B_.Count - 1
                Dim StrToCheck() As String = B_(I).Split(","c)
                Dim Count As Integer = 0
                For j As Integer = 0 To C_.Count - 1
                    For Each Check As String In StrToCheck
                        If Check.Contains(C_(j)) Then Count += 1
                    Next
                Next
                TextBox4.AppendText("Occurances (" & A_(I) & ")= " & Count.ToString & vbCrLf)
            Next
    
    

    I'm not sure you realize what that counting section is doing.

    The outer loop is on list B_ so it is going to count the number of occurrences
    in *each* *line* of B_  and output the count for each line. It is counting 
    how many variations of *all* numbers in list A_ occur in each line of B_.

    So the display will show how many times a variation of 768 and 836 occurs in
    the first line of B_, and then how many variations of 768 and 836 occurs in
    the second line of B_.

    If you change the display of the counts to show B_(I) instead of A_(I) you
    should have a clearer picture of what it is actually counting.

    - Wayne

    Monday, March 9, 2020 6:38 AM
  • Thank Wayne try to show me how it is.But I still cannot do.

     I try to find in first line in list B_ 

    768
    786
    687
    678
    876
    867

     I try to find in secondline in list B_ 

    836
    863
    368
    386
    683
    638

    I try replace A_ to C_ but not succeced.

            Dim sb As New StringBuilder
    
    
            Dim str As String = ""
    
    
            For I As Integer = 0 To A_.Count - 1
    
                Dim StringToCheck As String = A_(I).ToString
                Dim StringsToCheck As String() = StringToCheck.Split(","c)
    
                For Each S As String In StringsToCheck
    
                    '0 1 2
                    '0 2 1
                    '1 2 0
                    '1 0 2
                    '2 0 1
                    '2 1 0
    
                    Dim StringsToFind As String() = {S(0) & S(1) & S(2),
                                                     S(0) & S(2) & S(1),
                                                     S(1) & S(2) & S(0),
                                                     S(1) & S(0) & S(2),
                                                     S(2) & S(0) & S(1),
                                                     S(2) & S(1) & S(0)}
    
                    For J As Integer = 0 To StringsToFind.Count - 1
                        sb.Append(StringsToFind(J) & vbCrLf)
                        str += StringsToFind(J) & vbCrLf
                    Next
                Next
            Next
            Me.TextBox3.AppendText(sb.ToString)
            C_.Add(str)
    
            Dim str_ As String = ""
    
            For I As Integer = 0 To B_.Count - 1
                Dim StrToCheck() As String = B_(I).Split(","c)
                Dim Count As Integer = 0
                For j As Integer = 0 To C_.Count - 1
                    For Each Check As String In StrToCheck
                        If Check.Contains(C_(j)) Then Count += 1
                        str_ += C_(j) & Count
                    Next
                Next
    
            Next
    
            TextBox4.AppendText("Occurances (" & str_ & ")= " & vbCrLf)
    
    

    Monday, March 9, 2020 3:46 PM

  •  I try to find in first line in list B_ 

    768
    786
    687
    678
    876
    867

     I try to find in secondline in list B_ 

    836
    863
    368
    386
    683
    638


            Dim sb As New StringBuilder
    
    
            Dim str As String = ""
    
    
            For I As Integer = 0 To A_.Count - 1
    
                Dim StringToCheck As String = A_(I).ToString
                Dim StringsToCheck As String() = StringToCheck.Split(","c)
    
                For Each S As String In StringsToCheck
    
                    '0 1 2
                    '0 2 1
                    '1 2 0
                    '1 0 2
                    '2 0 1
                    '2 1 0
    
                    Dim StringsToFind As String() = {S(0) & S(1) & S(2),
                                                     S(0) & S(2) & S(1),
                                                     S(1) & S(2) & S(0),
                                                     S(1) & S(0) & S(2),
                                                     S(2) & S(0) & S(1),
                                                     S(2) & S(1) & S(0)}
    
                    For J As Integer = 0 To StringsToFind.Count - 1
                        sb.Append(StringsToFind(J) & vbCrLf)
                        str += StringsToFind(J) & vbCrLf
                    Next
                Next
            Next
            Me.TextBox3.AppendText(sb.ToString)
            C_.Add(str)
    
    

    Your description doesn't come close to what you are actually doing.

    From your description it appears that you want to find all combinations of the
    first number in List A_ which is 768 that occur in the first line in list B_
    which is:

    B_.Add("613,613,633,133,305,300,350,050,654,653,643,543,161,166,116,616,392,393,323,923,834,835,845,345,201,542,")

    Then find all combinations of the second number in List A_ which is 836 that
    occur in the second line in list B_ which is:

    B_.Add("635,638,658,358,142,142,122,422,694,696,646,946,429,427,497,297,824,822,078,242,543,547,201,437,386,678,")

    But in your code when you build list C_ you are creating all combinations of
    *all lines* in list A_ You are not processing them one line at a time.

    Before you even start checking list B_, there is one string in C_ which is:

    "768" & vbCrLf & "786" & vbCrLf & "687" & vbCrLf & "678" & vbCrLf & "876" & vbCrLf & "867" & vbCrLf & "836" & vbCrLf & "863" & vbCrLf & "368" & vbCrLf & "386" & vbCrLf & "683" & vbCrLf & "638" & vbCrLf

    - Wayne

    Monday, March 9, 2020 6:14 PM
  • Wayne, I still don't know to proses line A_ one line at a time.

            For I As Integer = 0 To A_.Count - 1
    
                Dim StringToCheck As String = A_(I).ToString
                Dim StringsToCheck As String() = StringToCheck.Split(","c)
    
                For Each S As String In StringsToCheck
    
                    '0 1 2
                    '0 2 1
                    '1 2 0
                    '1 0 2
                    '2 0 1
                    '2 1 0
    
                    Dim StringsToFind As String() = {S(0) & S(1) & S(2),
                                                     S(0) & S(2) & S(1),
                                                     S(1) & S(2) & S(0),
                                                     S(1) & S(0) & S(2),
                                                     S(2) & S(0) & S(1),
                                                     S(2) & S(1) & S(0)}
    
                    For J As Integer = 0 To StringsToFind.Count - 1
                        Me.TextBox3.AppendText(StringsToFind(J) & vbCrLf)
                        Dim Count As Integer = 0
                        For Each Check As String In B_
                            If Check.Contains(StringsToFind(J)) Then Count += 1
                        Next
                        TextBox4.AppendText(StringsToFind(J) & "=" & Count & vbCrLf)
                    Next
                Next
            Next
    

    help me

    Wednesday, March 11, 2020 1:32 PM
  •  I still don't know to proses line A_ one line at a time.

            For I As Integer = 0 To A_.Count - 1
    
                Dim StringToCheck As String = A_(I).ToString
                Dim StringsToCheck As String() = StringToCheck.Split(","c)
    
                For Each S As String In StringsToCheck
    
                    '0 1 2
                    '0 2 1
                    '1 2 0
                    '1 0 2
                    '2 0 1
                    '2 1 0
    
                    Dim StringsToFind As String() = {S(0) & S(1) & S(2),
                                                     S(0) & S(2) & S(1),
                                                     S(1) & S(2) & S(0),
                                                     S(1) & S(0) & S(2),
                                                     S(2) & S(0) & S(1),
                                                     S(2) & S(1) & S(0)}
    
                    For J As Integer = 0 To StringsToFind.Count - 1
                        Me.TextBox3.AppendText(StringsToFind(J) & vbCrLf)
                        Dim Count As Integer = 0
                        For Each Check As String In B_
                            If Check.Contains(StringsToFind(J)) Then Count += 1
                        Next
                        TextBox4.AppendText(StringsToFind(J) & "=" & Count & vbCrLf)
                    Next
                Next
            Next

    You still have not said what you are expecting as output, and in this latest 
    code you posted you haven't said or shown what the output is - and what is 
    wrong with it.

    We have no idea what you are trying to do. In your first post all you said was:

    "The error occur when we put the 678 in line2 in list B_"

    But you never said what the "error" is, so how are we supposed to know?

    You have been posting these little number diversions here for many months - or 
    is it years now? You should have learned by now how to post proper questions.
    You also should know by now how to make a simple comparison and display
    program.

    In all cases, whether simple or complex, you MUST state clearly and exactly
    what you are trying to accomplish. We can't and won't keep guessing.

    - Wayne

    Thursday, March 12, 2020 12:06 AM
  • sorry Wayne,

    I try explain again what I try to do

    1. List A_("768")

    Make combination in first line "786,687,678,876,867"

    2. List A_("836")

    Make combination in second line "863,368,386,683,638"

    3. List B_.Having 3 digit

    First line B_ ;

    "613,613,633,133,305,300,350,050,654,653,643,543,161,166,116,616,392,393,323,923,834,835,845,345,201,542,"

    second line B_ ;

    "635,638,658,358,142,142,122,422,694,696,646,946,429,427,497,297,824,822,078,242,543,547,201,437,386,411,"

    4.I want to find all combination in first line List A_in first line List B_ and then find all combination in second line List A_in second line List B_

    5. Output in textbox4 is

    768=0
    786=0
    687=0
    678=0
    876=0
    867=0

    result first line=0

    836=0
    863=0
    368=0
    386=1
    683=0
    638=1
    result first line=2

    Thank you very much Wayne.
    • Edited by mipakteh Thursday, March 12, 2020 2:51 AM add
    Thursday, March 12, 2020 2:50 AM
  • Note: The issue in this thread has been resolved in another thread started by 
    the OP on the same subject:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/73e44c1e-9478-4b7b-a7c9-8b7425c24120/match-string-2?forum=vbgeneral

    - Wayne

    • Proposed as answer by tommytwotrain Saturday, March 14, 2020 2:14 PM
    • Marked as answer by mipakteh Saturday, March 21, 2020 1:15 AM
    Saturday, March 14, 2020 7:06 AM