Results 1 to 6 of 6

Thread: count kay in my Dictionary

  1. #1
    Frenzied Member
    Join Date
    Mar 05
    Posts
    1,074

    count kay in my Dictionary

    I loop a txt file line by line with:

    Code:
    Dim f As Integer
        f = FreeFile
        Open Path For Input As #f
        Do While Not EOF(f)
            Line Input #f, tempstr
            If tempstr <> "" Then
                ...
             If Not MYDIC.Exists(COD) Then
                MYDIC.Add COD, DESCR_COD
            End If            
              ...
            End If
        Loop
    
        Close #f
    COD and DESCR_COD are to valiable filed from a mid function.

    I need, in addition of the dictionary array, to add a count how many Key are duplicate in the array, example:

    COD not existx ad +1 to Key COD (is the first one presence of COD in dictionary array)

    COD exists add +1 to the already existis Key, just filled with +1

    ecc...

    result if i found 12 var named AAAA, 2 for BBBB, 17 for CCCC:

    12 - AAAA
    2 - BBBB
    17 - CCCC

    I hope you ubdesrtand me.
    Tkx

  2. #2
    Frenzied Member SamOscarBrown's Avatar
    Join Date
    Aug 12
    Location
    NC, USA
    Posts
    1,549

    Re: count kay in my Dictionary

    THINK SO. You could put your COD string in an array, then when complete, go through your array and count the number of times each string appears. Like I said, I THINK I understand your issue. You work with arrays okay?

  3. #3
    PowerPoster
    Join Date
    Jun 01
    Location
    Trafalgar, IN
    Posts
    3,439

    Re: count kay in my Dictionary

    If you are just trying to figure out how to increment your counter then this should get you started. In your case, instead of using an array you would just put the the code to add or increment the dictionary inside your file reading code.
    Code:
    Private Sub Command1_Click()
    Dim arrList(9) As String
    Dim dict As Dictionary
    Dim i As Integer
    Dim intCnt As Integer
    Dim varEntry As Variant
    
        arrList(0) = "AAA"
        arrList(1) = "AAA"
        arrList(2) = "AAA"
        arrList(3) = "BBB"
        arrList(4) = "CCC"
        arrList(5) = "BBB"
        arrList(6) = "BBB"
        arrList(7) = "AAA"
        arrList(8) = "AAA"
        arrList(9) = "BBB"
        
        Set dict = New Dictionary
        
        For i = 0 To 9
            If dict.Exists(arrList(i)) Then
                intCnt = dict(arrList(i))
                dict(arrList(i)) = intCnt + 1
            Else
                dict.Add arrList(i), 1
            End If
        Next
        
        
        For Each varEntry In dict
            Debug.Print dict(varEntry) & " - " & varEntry
        Next varEntry
        
        Set dict = Nothing
        
    End Sub

  4. #4
    Frenzied Member
    Join Date
    Mar 05
    Posts
    1,074

    Re: count kay in my Dictionary

    Tks!

    but...

    COD is the code of Agency DESCR_COD is the description of Agency and i dont see that, or not?

    i need:

    12 - AAAA - Descr_of_AAAA
    2 - BBBB - Descr_of_BBBB
    17 - CCCC - Descr_of_CCCC


    Sorry to post my prob in wrong mode and in a terrible enghlish

    naturally all other way are welcomed
    Last edited by luca90; Oct 25th, 2012 at 11:45 AM.

  5. #5
    Frenzied Member
    Join Date
    Mar 05
    Posts
    1,074

    Re: count kay in my Dictionary

    Quote Originally Posted by MarkT View Post
    If you are just trying to figure out how to increment your counter then this should get you started. In your case, instead of using an array you would just put the the code to add or increment the dictionary inside your file reading code.
    Code:
    Private Sub Command1_Click()
    Dim arrList(9) As String
    Dim dict As Dictionary
    Dim i As Integer
    Dim intCnt As Integer
    Dim varEntry As Variant
    
        arrList(0) = "AAA"
        arrList(1) = "AAA"
        arrList(2) = "AAA"
        arrList(3) = "BBB"
        arrList(4) = "CCC"
        arrList(5) = "BBB"
        arrList(6) = "BBB"
        arrList(7) = "AAA"
        arrList(8) = "AAA"
        arrList(9) = "BBB"
        
        Set dict = New Dictionary
        
        For i = 0 To 9
            If dict.Exists(arrList(i)) Then
                intCnt = dict(arrList(i))
                dict(arrList(i)) = intCnt + 1
            Else
                dict.Add arrList(i), 1
            End If
        Next
        
        
        For Each varEntry In dict
            Debug.Print dict(varEntry) & " - " & varEntry
        Next varEntry
        
        Set dict = Nothing
        
    End Sub
    Tks!

    but...

    COD is the code of Agency DESCR_COD is the description of Agency and i dont see that, or not?

    i need:

    12 - AAAA - Descr_of_AAAA
    2 - BBBB - Descr_of_BBBB
    17 - CCCC - Descr_of_CCCC


    Sorry to post my prob in wrong mode and in a terrible enghlish

    naturally all other way are welcomed

  6. #6
    PowerPoster
    Join Date
    Jun 01
    Location
    Trafalgar, IN
    Posts
    3,439

    Re: count kay in my Dictionary

    I'll give it one more try. Add a class module to your project. Leave the default name Class1.

    Code for the class module (Class1)
    Code:
    Option Explicit
    
    Public itemCount As Integer
    Public itemDescripton As String
    Then your form code
    Code:
    Private Sub Command1_Click()
    Dim arrList(9) As String
    Dim dict As Dictionary
    Dim tItem As Class1
    Dim i As Integer
    Dim intCnt As Integer
    Dim varEntry As Variant
    
        arrList(0) = "AAA"
        arrList(1) = "AAA"
        arrList(2) = "AAA"
        arrList(3) = "BBB"
        arrList(4) = "CCC"
        arrList(5) = "BBB"
        arrList(6) = "BBB"
        arrList(7) = "AAA"
        arrList(8) = "AAA"
        arrList(9) = "BBB"
        
        Set dict = New Dictionary
        
        
        For i = 0 To 9
            If dict.Exists(arrList(i)) Then
                Set tItem = dict(arrList(i))
                tItem.itemCount = tItem.itemCount + 1
                Set dict(arrList(i)) = tItem
            Else
                Set tItem = New Class1
                tItem.itemCount = 1
                tItem.itemDescripton = "Description of " & arrList(i)
                dict.Add arrList(i), tItem
            End If
        Next
        
        
        For Each varEntry In dict
            Set tItem = dict(varEntry)
            Debug.Print tItem.itemCount & " - " & varEntry & " - " & tItem.itemDescripton
        Next varEntry
        
        Set dict = Nothing
        
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •