STRATEGY with dictionary

User avatar
sal21
PlatinumLounger
Posts: 3556
Joined: 26 Apr 2010, 17:36

STRATEGY with dictionary

Post by sal21 »

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!

User avatar
HansV
Administrator
Posts: 71155
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: STRATEGY with dictionary

Post by HansV »

No, I don't think so. If VALORE can have 9 different values, the code

Code: Select all

        If Not DICT.Exists(VALORE) Then
            DICT.Add VALORE, CONTA
            CONTA = CONTA + 1
        End If 
will produce a dictionary with 9 items, one for each unique value of VALORE.
Regards,
Hans

User avatar
sal21
PlatinumLounger
Posts: 3556
Joined: 26 Apr 2010, 17:36

Re: STRATEGY with dictionary

Post by sal21 »

HansV wrote:
02 May 2021, 09:20
No, I don't think so. If VALORE can have 9 different values, the code

Code: Select all

        If Not DICT.Exists(VALORE) Then
            DICT.Add VALORE, CONTA
            CONTA = CONTA + 1
        End If 
will produce a dictionary with 9 items, one for each unique value of VALORE.
hummm...
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

User avatar
HansV
Administrator
Posts: 71155
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: STRATEGY with dictionary

Post by HansV »

I'm afraid I don't understand.
Regards,
Hans

User avatar
sal21
PlatinumLounger
Posts: 3556
Joined: 26 Apr 2010, 17:36

Re: STRATEGY with dictionary

Post by sal21 »

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

User avatar
HansV
Administrator
Posts: 71155
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: STRATEGY with dictionary

Post by HansV »

OK, thanks. You will need to create 9 dictionaries for that, or an array of type Dictionary with 9 elements.
Regards,
Hans

User avatar
sal21
PlatinumLounger
Posts: 3556
Joined: 26 Apr 2010, 17:36

Re: STRATEGY with dictionary

Post by sal21 »

...
or an array of type Dictionary with 9 elements
have an example?

User avatar
HansV
Administrator
Posts: 71155
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: STRATEGY with dictionary

Post by HansV »

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.
Regards,
Hans

User avatar
sal21
PlatinumLounger
Posts: 3556
Joined: 26 Apr 2010, 17:36

Re: STRATEGY with dictionary

Post by sal21 »

HansV wrote:
02 May 2021, 10:55

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.
i need to set to 0 (Zero) before start the looping in txt?

for exampe:
CONTA1=0
CONTA2==
...
CONTA9=0

or not?

User avatar
HansV
Administrator
Posts: 71155
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: STRATEGY with dictionary

Post by HansV »

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.
Regards,
Hans

User avatar
sal21
PlatinumLounger
Posts: 3556
Joined: 26 Apr 2010, 17:36

Re: STRATEGY with dictionary

Post by sal21 »

HansV wrote:
02 May 2021, 14:45
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.
ahhhhh...
tks

User avatar
sal21
PlatinumLounger
Posts: 3556
Joined: 26 Apr 2010, 17:36

Re: STRATEGY with dictionary

Post by sal21 »

HansV wrote:
02 May 2021, 14:45
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.
And to the end of looping txt, how to loop each dictionary elements?

NOTE:
CreasteObject :grin:

s

User avatar
HansV
Administrator
Posts: 71155
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: STRATEGY with dictionary

Post by HansV »

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
Regards,
Hans

User avatar
sal21
PlatinumLounger
Posts: 3556
Joined: 26 Apr 2010, 17:36

Re: STRATEGY with dictionary

Post by sal21 »

:cheers: :clapping: :fanfare:
HansV wrote:
03 May 2021, 10:36
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