STRATEGY with dictionary
-
- PlatinumLounger
- Posts: 4355
- Joined: 26 Apr 2010, 17:36
STRATEGY with dictionary
i loop a txt line by line and get a sting (VALORE) with mid statement, during the loop, i fill a Dictionary whit:
...
If Not DICT.Exists(VALORE) Then
DICT.Add VALORE, CONTA
CONTA = CONTA + 1
End If
...
but my prob is to get a other string value for 9 other value!
i need to set 9 dictionary?
for example:
Set DICT1 = CreateObject("scripting.Dictionary")
Set DICT2 = CreateObject("scripting.Dictionary")
Set DICT3 = CreateObject("scripting.Dictionary")
...
Set DICT9 = CreateObject("scripting.Dictionary")
If Not DICT1.Exists(VALORE1) Then
DICT1.Add VALORE1, CONTA1
CONTA1 = CONTA1 + 1
End If
If Not DICT2.Exists(VALORE2) Then
DICT2.Add VALORE2, CONTA1
CONTA1 = CONTA1 + 1
End If
If Not DICT3.Exists(VALORE3) Then
DICT3.Add VALORE3, CONTA1
CONTA1 = CONTA1 + 1
End If
...
If Not DICT9.Exists(VALORE9) Then
DICT9.Add VALORE9, CONTA1
CONTA1 = CONTA1 + 1
End If
help!
...
If Not DICT.Exists(VALORE) Then
DICT.Add VALORE, CONTA
CONTA = CONTA + 1
End If
...
but my prob is to get a other string value for 9 other value!
i need to set 9 dictionary?
for example:
Set DICT1 = CreateObject("scripting.Dictionary")
Set DICT2 = CreateObject("scripting.Dictionary")
Set DICT3 = CreateObject("scripting.Dictionary")
...
Set DICT9 = CreateObject("scripting.Dictionary")
If Not DICT1.Exists(VALORE1) Then
DICT1.Add VALORE1, CONTA1
CONTA1 = CONTA1 + 1
End If
If Not DICT2.Exists(VALORE2) Then
DICT2.Add VALORE2, CONTA1
CONTA1 = CONTA1 + 1
End If
If Not DICT3.Exists(VALORE3) Then
DICT3.Add VALORE3, CONTA1
CONTA1 = CONTA1 + 1
End If
...
If Not DICT9.Exists(VALORE9) Then
DICT9.Add VALORE9, CONTA1
CONTA1 = CONTA1 + 1
End If
help!
-
- Administrator
- Posts: 78471
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: STRATEGY with dictionary
No, I don't think so. If VALORE can have 9 different values, the code
will produce a dictionary with 9 items, one for each unique value of VALORE.
Code: Select all
If Not DICT.Exists(VALORE) Then
DICT.Add VALORE, CONTA
CONTA = CONTA + 1
End If
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4355
- Joined: 26 Apr 2010, 17:36
Re: STRATEGY with dictionary
hummm...HansV wrote: ↑02 May 2021, 09:20No, I don't think so. If VALORE can have 9 different values, the code
will produce a dictionary with 9 items, one for each unique value of VALORE.Code: Select all
If Not DICT.Exists(VALORE) Then DICT.Add VALORE, CONTA CONTA = CONTA + 1 End If
but to the and of looping, i need to fill a 9 different combobox with unique value of valore, valore1... valore9...
for example combobox1 for valore, combobox2 for valore1..., combobox9 for valore9
-
- Administrator
- Posts: 78471
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
-
- PlatinumLounger
- Posts: 4355
- Joined: 26 Apr 2010, 17:36
Re: STRATEGY with dictionary
in mid(mystring, 1,25) position have a value for VALORE i need to fill combobox1 with unique value of VALORE
in mid(mystring, 4,9) position have a value for VALORE1 i need to fill combobox2 with unique value of VALORE1
...
in mid(mystring, ,32) position have a value for VALORE9 i need to fill combobox9 with unique value of VALORE9
in mid(mystring, 4,9) position have a value for VALORE1 i need to fill combobox2 with unique value of VALORE1
...
in mid(mystring, ,32) position have a value for VALORE9 i need to fill combobox9 with unique value of VALORE9
-
- Administrator
- Posts: 78471
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: STRATEGY with dictionary
OK, thanks. You will need to create 9 dictionaries for that, or an array of type Dictionary with 9 elements.
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4355
- Joined: 26 Apr 2010, 17:36
Re: STRATEGY with dictionary
...
or an array of type Dictionary with 9 elements
have an example?
or an array of type Dictionary with 9 elements
have an example?
-
- Administrator
- Posts: 78471
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: STRATEGY with dictionary
Code: Select all
Dim Arr(1 To 9) As Object
Dim i As Long
For i = 1 To 9
Set Arr(i) = CreasteObject(Class:="Scripting.Dictionary")
Next i
'...
If Not Arr(1).Exists(VALORE1) Then
Arr(1).Add Key:=VALORE1, Item:=CONTA1
CONTA1 = CONTA1 + 1
End If
If Not Arr(2).Exists(VALORE2) Then
Arr(2).Add Key:=VALORE2, Item:=CONTA2
CONTA2 = CONTA2 + 1
End If
' etc.
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4355
- Joined: 26 Apr 2010, 17:36
Re: STRATEGY with dictionary
i need to set to 0 (Zero) before start the looping in txt?HansV wrote: ↑02 May 2021, 10:55Code: Select all
Dim Arr(1 To 9) As Object Dim i As Long For i = 1 To 9 Set Arr(i) = CreasteObject(Class:="Scripting.Dictionary") Next i '... If Not Arr(1).Exists(VALORE1) Then Arr(1).Add Key:=VALORE1, Item:=CONTA1 CONTA1 = CONTA1 + 1 End If If Not Arr(2).Exists(VALORE2) Then Arr(2).Add Key:=VALORE2, Item:=CONTA2 CONTA2 = CONTA2 + 1 End If ' etc.
for exampe:
CONTA1=0
CONTA2==
...
CONTA9=0
or not?
-
- Administrator
- Posts: 78471
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: STRATEGY with dictionary
No, that isn't necessary.
As the code is now, it'll set CONTA1 to 0 in Arr(1).Add Key:=VALORE1, Item:=CONTA1, etc.
As the code is now, it'll set CONTA1 to 0 in Arr(1).Add Key:=VALORE1, Item:=CONTA1, etc.
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4355
- Joined: 26 Apr 2010, 17:36
-
- PlatinumLounger
- Posts: 4355
- Joined: 26 Apr 2010, 17:36
-
- Administrator
- Posts: 78471
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: STRATEGY with dictionary
For example:
Code: Select all
Dim i As Long
Dim MyKey As Variant
For i = 1 to 9
For Each MyKey In Arr(i).Keys
Debug.Print MyKey
Next MyKey
Next i
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4355
- Joined: 26 Apr 2010, 17:36
Re: STRATEGY with dictionary
HansV wrote: ↑03 May 2021, 10:36For example:
Code: Select all
Dim i As Long Dim MyKey As Variant For i = 1 to 9 For Each MyKey In Arr(i).Keys Debug.Print MyKey Next MyKey Next i