|
-
Oct 25th, 2012, 10:09 AM
#1
Thread Starter
PowerPoster
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
-
Oct 25th, 2012, 10:20 AM
#2
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?
-
Oct 25th, 2012, 10:50 AM
#3
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
-
Oct 25th, 2012, 11:42 AM
#4
Thread Starter
PowerPoster
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.
-
Oct 25th, 2012, 11:46 AM
#5
Thread Starter
PowerPoster
Re: count kay in my Dictionary
 Originally Posted by MarkT
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
-
Oct 25th, 2012, 12:19 PM
#6
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
-
Aug 7th, 2013, 05:43 AM
#7
Thread Starter
PowerPoster
Re: count kay in my Dictionary
 Originally Posted by MarkT
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
hi friend tks and sorry for delay...
but into the for next is possible to retrive the occurrence for each key?
for example AAAA have 5 occurrence, BBBB have 4 occurrence, ecc....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|