none
Compare the same line RRS feed

  • Question

  • Hi All,

    How to compare each line having the same value then show in textbox3.If that line finish comparing then don't repeat compare.

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Imports System.IO
    Imports System.Data
    
    Public Class Form1
        Dim ETD As New List(Of String)
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1, 1, 1, 1, 1, 1, 2, 2," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,2 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,3 ,4 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,2 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,1 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,2 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,2 ,4 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,2 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,2 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,1 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,2 ,2 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,2 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,3 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,2 ,2 ,2 ,")
    
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            For I As Integer = 0 To ETD.Count - 1
                TextBox1.AppendText(ETD(I))
            Next
    
        End Sub
    
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    
            For I As Integer = 0 To ETD.Count - 2
                TextBox2.AppendText("Comparing Line " & I.ToString & " : " & ETD(I).ToString)
                For J As Integer = I + 1 To ETD.Count - 1
                    TextBox2.AppendText("To           " & J.ToString & " : " & ETD(J).ToString)
                    If ETD(I).ToString = ETD(J).ToString Then
                        TextBox3.AppendText(ETD(I).ToString)
                    End If
                Next
            Next
    
        End Sub
    
      
    End Class


    • Edited by mipakteh Sunday, September 17, 2017 1:03 PM delete
    Sunday, September 17, 2017 1:03 PM

Answers

  • I think better using Sort all lines method .

    I think what you really want to do is to group the lines, count the number in each group, and then display them according to the count - display groups with more than one item in the group separately from groups with exactly one item in the group. It is a counting task, not a matching task.

    It is a pity that you did not properly describe your task at the start, as the code you have will not be suitable for what you want. The code that will create the groups and display them separately, according to the count, is:

        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            Dim Result = From Str In ETD
                             Order By Str
                             Group By Groups = Str
                             Into GroupCounts = Group, Count()
            For Each obj As Object In Result
                If obj.count > 1 Then
                    For I As Integer = 1 To obj.count
                        TextBox3.AppendText(obj.groups)
                    Next I
                Else
                    ' put to somewhere
                    TextBox4.AppendText(obj.groups)
                End If
            Next
        End Sub

    You should consider how often the example you have provided has been wrong.  This is because you do not properly describe what you want to do, so the result you expect is just a guess, and it is misleading and time wasting.

    • Marked as answer by mipakteh Saturday, September 23, 2017 5:40 AM
    Friday, September 22, 2017 10:53 AM

All replies

  • Hi

    If I understand your question.

    First of all, you need to study and understand the code, not just copy paste and then ask a new question that has already been answered. IronRazers has already provided the answer to this question in your last post.

    If you understand his answer you wouldn't have needed to ask this one.

    Here is IronRazerz code adapted to your new question.

    ' Form1 with
    ' Multiline TextBox1 (original list)
    ' Multiline TextBox2 (results list)
    ' TextBox3 for searxch string
    ' Button1 to start search
    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim ETD As New List(Of String)
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ,")
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ,")
            ETD.Add("1, 1, 1, 1, 1, 1, 2, 2,")
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ,")
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ,")
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ,")
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ,")
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ,")
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,2 ,")
            ETD.Add("1 ,1 ,1 ,1 ,2 ,2 ,2 ,")
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ,")
            ETD.Add("1 ,1 ,1 ,3 ,4 ,")
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ,")
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ,")
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ,")
            ETD.Add("1 ,2 ,2 ,2 ,3 ,")
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ,")
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,1 ,3 ,")
            ETD.Add("1 ,1 ,1 ,1 ,2 ,2 ,2 ,")
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ,")
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ,")
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ,")
            ETD.Add("1 ,1 ,1 ,1 ,2 ,4 ,")
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ,")
            ETD.Add("1 ,1 ,1 ,1 ,2 ,2 ,2 ,")
            ETD.Add("1 ,1 ,1 ,1 ,2 ,2 ,2 ,")
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ,")
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ,")
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,1 ,3 ,")
            ETD.Add("1 ,1 ,2 ,2 ,2 ,2 ,")
            ETD.Add("1 ,1 ,1 ,1 ,2 ,2 ,2 ,")
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ,")
            ETD.Add("1 ,1 ,1 ,1 ,3 ,3 ,")
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ,")
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ,")
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ,")
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ,")
            ETD.Add("1 ,1 ,1 ,1 ,2 ,2 ,2 ,")
    
            Dim sb As New Text.StringBuilder
            For Each s As String In ETD
                sb.AppendLine(s)
            Next
            TextBox1.AppendText(sb.ToString)
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim sb As New Text.StringBuilder
            For Each s As String In ETD
                If s.StartsWith(TextBox3.Text) Then
                     If Not sb.ToString.Contains(s) Then sb.AppendLine(s)
                End If
            Next
            TextBox2.Text = sb.ToString
        End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Sunday, September 17, 2017 2:20 PM
    Sunday, September 17, 2017 1:40 PM
  • Hi Leshay,

    I think it very different leshay.IronRazerz answer my question very perfect.

    In click Button ,Find the string as a line. But for compare, I try to use this code for compare the same value in line

     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    
            For I As Integer = 0 To ETD.Count - 2
                TextBox2.AppendText("Comparing Line " & I.ToString & " : " & ETD(I).ToString)
                For J As Integer = I + 1 To ETD.Count - 1
                    TextBox2.AppendText("To           " & J.ToString & " : " & ETD(J).ToString)
                    If ETD(I).ToString = ETD(J).ToString Then
                        TextBox3.AppendText(ETD(I).ToString)
                    End If
                Next
            Next
    
        End Sub

    What problem in this code is comparing occur twice,for example see the line "1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ,".

    It should be compare only one from line 0 to the end line.


    • Edited by mipakteh Sunday, September 17, 2017 2:01 PM add
    Sunday, September 17, 2017 2:00 PM
  •  You are not making your question clear enough for me to understand.  Going from your example,  show us the search string you want found.  Then show us what you expect to have shown in TextBox3.  A visual of the outcome wanted will help a lot.

     The better the information is that you give us,  the easier we can help.


    If you say it can`t be done then i`ll try it

    Sunday, September 17, 2017 2:14 PM
  • What problem in this code is comparing occur twice,for example see the line "1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ,".

    It should be compare only one from line 0 to the end line.

    The code you have posted will compare each line with all others.  It will show a result for every match.

    Please show the result you expect to get from that data, including the I and J values.  It is not clear what lines you want to compare.

    Sunday, September 17, 2017 2:19 PM
  • Hi

    OK, obviously, I did not understand your question. I still really do not understand, but I *think* you are wanting to just find one occurrance of the search string?

    If so, then try the code I posted above - it probably isn't the answer, but it is the best I can do for now.

    The code above has been amended to reflect my current thinking.

    EDIT: perhaps, based on the example data you provided in 'ETD', please show your expected result from a few different seach strings.


    Regards Les, Livingston, Scotland


    • Edited by leshay Sunday, September 17, 2017 2:28 PM Added an EDIT question
    Sunday, September 17, 2017 2:26 PM
  • Thank you very Leshay,If possible can do with sort method ?.

    How can I do sort all line?.

    Sunday, September 17, 2017 2:38 PM
  • Thank you very Leshay,If possible can do with sort method ?.

    How can I do sort all line?.

    Hi

    Please provide requested information (mine and other asked)

    EDIT: perhaps, based on the example data you provided in 'ETD', please show your expected result from a few different seach strings.


    Regards Les, Livingston, Scotland

    Sunday, September 17, 2017 2:41 PM
  • In Textbox3 should be show 5 of line "1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ,".

    My question is why only 4 after comparing using this code;

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    
            For I As Integer = 0 To ETD.Count - 2
                TextBox2.AppendText("Comparing Line " & I.ToString & " : " & ETD(I).ToString)
                For J As Integer = I + 1 To ETD.Count - 1
                    TextBox2.AppendText("To           " & J.ToString & " : " & ETD(J).ToString)
                    If ETD(I).ToString = ETD(J).ToString Then
                        TextBox3.AppendText(ETD(I).ToString)
                    End If
                Next
            Next
    
        End Sub

    Sunday, September 17, 2017 3:17 PM
  • Hi

    To answer My question is why only 4 after comparing using this code;

    Check the contents of each of the strings you assigned to ETD! The code does find the 4 and only 4 matches! You, a mere human, being more fallable than a superior computer, made an incorrect comparison.

    (Hint: look for a missing space character in the second occurrance)

    EDIT: why do you set

    For I As Integer = 0 To ETD.Count - 2

    You *may* benefit from using a function - such as:

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            TextBox2.Clear()
            For Each s As String In ETD
                TextBox2.AppendText(FindString(s))
            Next
        End Sub
        Function FindString(search As String) As String
            Dim sb As New Text.StringBuilder
            For Each s As String In ETD
                If s.StartsWith(search) Then
                    sb.AppendLine(s)
                End If
            Next
            Return sb.ToString
        End Function


    Regards Les, Livingston, Scotland






    • Edited by leshay Sunday, September 17, 2017 4:11 PM
    Sunday, September 17, 2017 4:02 PM
  • My question is why only 4 after comparing using this code

    Because you are comparing strings created from the arrays, instead of comparing the arrays.

    Why have you turned the arrays into strings?  That is going to create errors.  If you compare the original arrays as arrays, and you will not have this sort of problem.    You should not be using the textboxes as the source of the data - you should be using the information from the original class instances.   This technique of creating textboxes and trying to compare or analyse lines of text is something you have tried before, and it created this sort of error.  You must work with the original data, not with strings.

    Sunday, September 17, 2017 9:53 PM
  • Hi Leshay,

    I give a new List for small example.To show the expected result in Textbox3

    TextBox3,

    Then code;

    Option Strict On Option Explicit On Option Infer Off Imports System.IO Imports System.Data Public Class Form1 Dim ETD As New List(Of String) Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load ETD.Add("1 ,1 ,2 ,2 ," & vbCrLf) ETD.Add("1 ,2 ,2 ,3 ," & vbCrLf) ETD.Add("1 ,1 ,1 ,1 ," & vbCrLf) ETD.Add("1 ,1 ,2 ,2 ," & vbCrLf) ETD.Add("1 ,1 ,2 ,2 ," & vbCrLf) ETD.Add("1 ,2 ,2 ,3 ," & vbCrLf) ETD.Add("1 ,1 ,1 ,1 ," & vbCrLf) ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf) ETD.Add("1 ,2 ,2 ,3 ," & vbCrLf) End Sub Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click For I As Integer = 0 To ETD.Count - 1 TextBox1.AppendText(ETD(I)) Next End Sub Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click For I As Integer = 0 To ETD.Count - 2 TextBox2.AppendText("Comparing Line " & I.ToString & " : " & ETD(I).ToString) For J As Integer = I + 1 To ETD.Count - 1 TextBox2.AppendText("To " & J.ToString & " : " & ETD(J).ToString) If ETD(I).ToString = ETD(J).ToString Then TextBox3.AppendText(ETD(I).ToString) End If Next Next End Sub Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click

    End Sub End Class



    • Edited by mipakteh Friday, September 22, 2017 2:31 AM Repair
    Friday, September 22, 2017 2:29 AM
  • I give a new List for small example.To show the expected result in Textbox3

    That doesn't make sense.

    Item 0 (1,1,2,2) occurs in the text box three times because there are three comparisons for which there is a match - Item 0 with Item 3, Item 0 with Item 4, and item 3 with item 4.  So the rule is "Each time there is a match, show the matched value"

    So the list would be:

    1 ,1 ,2 ,2 Item 0 == Item 3
    1 ,1 ,2 ,2 Item 0 == Item 4
    1 ,1 ,2 ,2 Item 3 == Item 4

    1 ,2 ,2 ,3 Item 1 == Item 5
    1 ,2 ,2 ,3 Item 1 == Item 8
    1 ,2 ,2 ,3 Item 5 == Item 8

    1 ,1 ,1 ,1 Item 2 == Item 6

    So why are you also displaying 1,1,1,2,2,3?  That item doesn't match anything.

    It is not possible to work out your rule from the example, and without knowing your rule it is not possible to work out what the code should be.


    • Edited by Acamar Friday, September 22, 2017 3:14 AM fmt
    Friday, September 22, 2017 3:13 AM
  • Sorry Acamar, my mistake .That expected result show in Textbox3 is;

    So why are you also displaying 1,1,1,2,2,3?  That item doesn't match anything.

    ;Because for the future machine can produce the new value.

    Friday, September 22, 2017 3:46 AM
  • ;Because for the future machine can produce the new value.

    I don't know what that means.

    Should 1,1,1,2,2,3 be displayed or not?   What is the rule for the items to display?

     
    Friday, September 22, 2017 4:01 AM
  • Should be dispaly Acamar,If not we can't compare for the next time.

    Acamar, Let said;

    If matching then show in Textbox3,If not skip that item but put to another place

    Friday, September 22, 2017 5:08 AM
  • If matching then show in Textbox3,If not skip that item but put to another place

    Then you need to 'put to another place' using the Else:

    For I As Integer = 0 To ETD.Count - 2 For J As Integer = I + 1 To ETD.Count - 1 TextBox2.AppendText("To " & J.ToString & " : " & ETD(J).ToString) If ETD(I) = ETD(J) Then TextBox3.AppendText(ETD(I))
    Else
    ' Put to another place End If Next Next

    Friday, September 22, 2017 6:21 AM
  • thank Acamar...

    I think better using Sort all lines method .


    Friday, September 22, 2017 8:36 AM
  • I think better using Sort all lines method .

    I think what you really want to do is to group the lines, count the number in each group, and then display them according to the count - display groups with more than one item in the group separately from groups with exactly one item in the group. It is a counting task, not a matching task.

    It is a pity that you did not properly describe your task at the start, as the code you have will not be suitable for what you want. The code that will create the groups and display them separately, according to the count, is:

        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            Dim Result = From Str In ETD
                             Order By Str
                             Group By Groups = Str
                             Into GroupCounts = Group, Count()
            For Each obj As Object In Result
                If obj.count > 1 Then
                    For I As Integer = 1 To obj.count
                        TextBox3.AppendText(obj.groups)
                    Next I
                Else
                    ' put to somewhere
                    TextBox4.AppendText(obj.groups)
                End If
            Next
        End Sub

    You should consider how often the example you have provided has been wrong.  This is because you do not properly describe what you want to do, so the result you expect is just a guess, and it is misleading and time wasting.

    • Marked as answer by mipakteh Saturday, September 23, 2017 5:40 AM
    Friday, September 22, 2017 10:53 AM
  • Very very goooood.Acamar.

    I'm very sorry  It is a counting task, not a matching task. 

    Saturday, September 23, 2017 5:40 AM