none
KeyValuePair RRS feed

  • Question

  • Hi All,

    I have 3 line string from text file.We count a digit from 0 to 9 for every line not for all lines togehter

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Imports System.IO
    Imports System.Data
    
    Public Class Form1
    
        Private Bucket As New List(Of String)
        Private Lines As New List(Of String)
    
        Dim Result As New Dictionary(Of String, Integer)
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            TextBox1.Clear()
            TextBox2.Clear()
    
            'tox text file= "6005,1426,1385,5732,2229,3699,9950,8881,6223,0893,7574,3507,7538,4630,6523,2446,2223,5198,1997,4966,3500,0590,7527"
            '               "8992,4563,6716,8222,5077,7888,5657,1359,7325,1379,5359,9863,4108,4404,2832,5788,6083,2505,5301,9701,6266,7672,1792"
            '               "1344,7917,2893,4315,1381,6425,4200,3574,7456,1910,9244,6718,9413,1915,1590,5906,2135,2878,1951,0434,7359,1369,3184"
    
            Dim sr As New System.IO.StreamReader("C:\Users\family\Documents\Toc_.txt")
            Dim counter As New Dictionary(Of String, Integer)
    
            Do While sr.Peek <> -1
    
                Dim TCount As String = sr.ReadLine()
    
                For i As Integer = 0 To 9
                    Dim StringToFind As String = CStr(i)
                    Dim TResult_ As String = TCount.Count(Function(c As Char) c = StringToFind).ToString
    
                    TextBox1.AppendText(CStr(i) & "  Is   " & TResult_ & " " & vbCrLf)
    
                    Bucket.Add(TResult_)
                Next
                For J As Integer = 0 To TCount.Count - 1
                    Dim IDLine As String = TCount(J)
                    Lines.Add(IDLine)
                    TextBox1.AppendText(Lines(J))
                Next
    
            Loop
    
        End Sub
    
        Public Sub ShowList(ItemList As Dictionary(Of String, Integer))
            TextBox2.Clear()
            For Each KV As KeyValuePair(Of String, Integer) In ItemList
                TextBox2.AppendText(KV.Key & "=" & KV.Value.ToString & vbCrLf)
            Next
        End Sub
    
    
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            For Each Item As String In Bucket
                If Result.Keys.Contains(Item) Then
                    Result(Item) += 1
                Else
                    Result.Add(Item, 1)
                End If
            Next
            ShowList(Result)
    
        End Sub
    End Class
    

    .

    When we get that all digits for every line , Then try to find a Pair Value using KeyValuePair for every line ,How.

    The result must show like this;

    Saturday, July 22, 2017 5:33 AM

Answers

  • When we get that all digits for every line , Then try to find a Pair Value using KeyValuePair for every line ,How.

    Put it in a loop, as was recommended the last time that you asked this question.

    Option Strict On
    
    Imports System.IO
    Imports System.Data
    
    Public Class Form1
    
        Private Buckets As New List(Of List(Of String))
        Private Lines As New List(Of String)
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            TextBox1.Clear()
            TextBox2.Clear()
    
            'tox text file= "6005,1426,1385,5732,2229,3699,9950,8881,6223,0893,7574,3507,7538,4630,6523,2446,2223,5198,1997,4966,3500,0590,7527"
            '               "8992,4563,6716,8222,5077,7888,5657,1359,7325,1379,5359,9863,4108,4404,2832,5788,6083,2505,5301,9701,6266,7672,1792"
            '               "1344,7917,2893,4315,1381,6425,4200,3574,7456,1910,9244,6718,9413,1915,1590,5906,2135,2878,1951,0434,7359,1369,3184"
    
            Dim sr As New System.IO.StreamReader("<insert your file path here>")
          
            Do While sr.Peek <> -1
                Dim Bucket As New List(Of String)
                Dim TCount As String = sr.ReadLine()
    
                For i As Integer = 0 To 9
                    Dim StringToFind As String = CStr(i)
                    Dim TResult_ As String = TCount.Count(Function(c As Char) c = StringToFind).ToString
    
                    TextBox1.AppendText(CStr(i) & "  Is   " & TResult_ & " " & vbCrLf)
    
                    Bucket.Add(TResult_)
                Next
                For J As Integer = 0 To TCount.Count - 1
                    Dim IDLine As String = TCount(J)
                    Lines.Add(IDLine)
                    TextBox1.AppendText(Lines(J))
                Next
                Buckets.Add(Bucket)
            Loop
    
        End Sub
    
        Public Sub ShowList(ItemList As Dictionary(Of String, Integer))
            For Each KV As KeyValuePair(Of String, Integer) In ItemList
                TextBox2.AppendText(KV.Key & "=" & KV.Value.ToString & vbCrLf)
            Next
            TextBox2.AppendText(vbCrLf)
        End Sub
    
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            TextBox2.Clear()
            For Each bucket As List(Of String) In Buckets
                Dim Result As New Dictionary(Of String, Integer)
                For Each Item As String In bucket
                    If Result.Keys.Contains(Item) Then
                        Result(Item) += 1
                    Else
                        Result.Add(Item, 1)
                    End If
                Next
                ShowList(Result)
            Next
        End Sub
    
    End Class
    • Marked as answer by mipakteh Saturday, July 22, 2017 3:42 PM
    Saturday, July 22, 2017 6:18 AM
  • I think this code loop more then one but I don't where is mistake.

    Please look again at what I posted.  "Then you don't need the extra loop for the button2 code.  Just put the original button2 code inside the loop ..." (emphasis added).

    • Marked as answer by mipakteh Sunday, July 23, 2017 6:48 AM
    • Unmarked as answer by mipakteh Wednesday, August 2, 2017 3:20 AM
    • Marked as answer by mipakteh Wednesday, August 2, 2017 3:20 AM
    Sunday, July 23, 2017 6:13 AM

All replies

  • When we get that all digits for every line , Then try to find a Pair Value using KeyValuePair for every line ,How.

    Put it in a loop, as was recommended the last time that you asked this question.

    Option Strict On
    
    Imports System.IO
    Imports System.Data
    
    Public Class Form1
    
        Private Buckets As New List(Of List(Of String))
        Private Lines As New List(Of String)
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            TextBox1.Clear()
            TextBox2.Clear()
    
            'tox text file= "6005,1426,1385,5732,2229,3699,9950,8881,6223,0893,7574,3507,7538,4630,6523,2446,2223,5198,1997,4966,3500,0590,7527"
            '               "8992,4563,6716,8222,5077,7888,5657,1359,7325,1379,5359,9863,4108,4404,2832,5788,6083,2505,5301,9701,6266,7672,1792"
            '               "1344,7917,2893,4315,1381,6425,4200,3574,7456,1910,9244,6718,9413,1915,1590,5906,2135,2878,1951,0434,7359,1369,3184"
    
            Dim sr As New System.IO.StreamReader("<insert your file path here>")
          
            Do While sr.Peek <> -1
                Dim Bucket As New List(Of String)
                Dim TCount As String = sr.ReadLine()
    
                For i As Integer = 0 To 9
                    Dim StringToFind As String = CStr(i)
                    Dim TResult_ As String = TCount.Count(Function(c As Char) c = StringToFind).ToString
    
                    TextBox1.AppendText(CStr(i) & "  Is   " & TResult_ & " " & vbCrLf)
    
                    Bucket.Add(TResult_)
                Next
                For J As Integer = 0 To TCount.Count - 1
                    Dim IDLine As String = TCount(J)
                    Lines.Add(IDLine)
                    TextBox1.AppendText(Lines(J))
                Next
                Buckets.Add(Bucket)
            Loop
    
        End Sub
    
        Public Sub ShowList(ItemList As Dictionary(Of String, Integer))
            For Each KV As KeyValuePair(Of String, Integer) In ItemList
                TextBox2.AppendText(KV.Key & "=" & KV.Value.ToString & vbCrLf)
            Next
            TextBox2.AppendText(vbCrLf)
        End Sub
    
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            TextBox2.Clear()
            For Each bucket As List(Of String) In Buckets
                Dim Result As New Dictionary(Of String, Integer)
                For Each Item As String In bucket
                    If Result.Keys.Contains(Item) Then
                        Result(Item) += 1
                    Else
                        Result.Add(Item, 1)
                    End If
                Next
                ShowList(Result)
            Next
        End Sub
    
    End Class
    • Marked as answer by mipakteh Saturday, July 22, 2017 3:42 PM
    Saturday, July 22, 2017 6:18 AM
  • Thank you very much Acamar,Code working correctly.

    If we want put all List in bellow at one place.Just for to know how to re-use back it.

    TextBox1.AppendText(CStr(i) & "  Is   " & TResult_ & " " & vbCrLf)

     TextBox1.AppendText(Lines(J))

    TextBox2.AppendText(KV.Key & "=" & KV.Value.ToString & vbCrLf)

    Then look like this;

    Saturday, July 22, 2017 3:42 PM
  • If we want put all List in bellow at one place.Just for to know how to re-use back it.

    Then you don't need the extra loop for the button2 code.  Just put the original button2 code inside the loop that calculates the bucket in the button1 click, and process the bucket each time it is created.

    Saturday, July 22, 2017 10:33 PM
  • I think this code loop more then one but I don't where is mistake.

    Option Strict On
    
    Imports System.IO
    Imports System.Data
    
    
    Public Class Form1
    
        Private Buckets As New List(Of List(Of String))
        Private Lines As New List(Of String)
        Private Digit As New List(Of String)
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            TextBox1.Clear()
    
    
            'tox text file= "6005,1426,1385,5732,2229,3699,9950,8881,6223,0893,7574,3507,7538,4630,6523,2446,2223,5198,1997,4966,3500,0590,7527"
            '               "8992,4563,6716,8222,5077,7888,5657,1359,7325,1379,5359,9863,4108,4404,2832,5788,6083,2505,5301,9701,6266,7672,1792"
            '               "1344,7917,2893,4315,1381,6425,4200,3574,7456,1910,9244,6718,9413,1915,1590,5906,2135,2878,1951,0434,7359,1369,3184"
    
            Dim sr As New System.IO.StreamReader("C:\Users\family\Documents\Toc.txt")
    
    
            Do While sr.Peek <> -1
                Dim Bucket As New List(Of String)
                Dim TCount As String = sr.ReadLine()
    
                For i As Integer = 0 To 9
                    Dim StringToFind As String = CStr(i)
                    Dim TResult_ As String = TCount.Count(Function(c As Char) c = StringToFind).ToString
    
                    TextBox1.AppendText(CStr(i) & "  Is   " & TResult_ & " " & vbCrLf)
    
                    Bucket.Add(TResult_)
                    Digit.Add(CStr(i) & "  Is   " & TResult_ & " " & vbCrLf)
    
                Next
                For J As Integer = 0 To TCount.Count - 1
                    Dim IDLine As String = TCount(J)
                    Lines.Add(IDLine)
                    TextBox1.AppendText(IDLine)
                Next
                TextBox1.AppendText(vbCrLf)
    
                Buckets.Add(Bucket)
    
                For Each T As List(Of String) In Buckets
                    Dim Result As New Dictionary(Of String, Integer)
                    For Each Item As String In T
                        If Result.Keys.Contains(Item) Then
                            Result(Item) += 1
                        Else
                            Result.Add(Item, 1)
                        End If
                    Next
                    ShowList(Result)
                Next
    
            Loop
    
        End Sub
    
        Public Sub ShowList(ItemList As Dictionary(Of String, Integer))
            For Each KV As KeyValuePair(Of String, Integer) In ItemList
                TextBox1.AppendText(KV.Key & "=" & KV.Value.ToString & " ,")
            Next
            TextBox1.AppendText(vbCrLf)
        End Sub
    
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    
        End Sub
    
    End Class
    

    Sunday, July 23, 2017 5:30 AM
  • I think this code loop more then one but I don't where is mistake.

    Please look again at what I posted.  "Then you don't need the extra loop for the button2 code.  Just put the original button2 code inside the loop ..." (emphasis added).

    • Marked as answer by mipakteh Sunday, July 23, 2017 6:48 AM
    • Unmarked as answer by mipakteh Wednesday, August 2, 2017 3:20 AM
    • Marked as answer by mipakteh Wednesday, August 2, 2017 3:20 AM
    Sunday, July 23, 2017 6:13 AM
  • It ok...Confuse bucket, Bucket and buckets.

              Buckets.Add(Bucket)
    
    
                Dim Result As New Dictionary(Of String, Integer)
    
                For Each Item As String In Bucket
                    If Result.Keys.Contains(Item) Then
                        Result(Item) += 1
                    Else
                        Result.Add(Item, 1)
                    End If
                Next
                ShowList(Result)
    
            Loop

    Sunday, July 23, 2017 6:48 AM