Using Scripting dictionary in Word, it seems that a retrieval of a
non-existant item creates an item. In the code below the persons.count
= 1 after the failed retrieval. How would one lookup an item and add
one if not in the dictionary? Thanks.

Sub dictionarytest()
Dim c(2)
Dim persons As Object
Set persons = CreateObject("Scripting.Dictionary")
Dim keys As Variant, dcount As Long

On Error Resume Next
' This statement seems to add the item "foo" to the dictionary -
why?
dcount = persons("foo")(2)
On Error GoTo 0
dcount = persons.Count
MsgBox ("count1 = " & dcount)
c(1) = "foo"
dcount = persons.Count + 1
c(2) = dcount
' and it bombs here because "foo" is already in the dictionary
persons.Add "foo", c
End Sub

Re: Dictionary Objects - Looking up an item creates an item? by Steve

Steve
Sun Jun 04 17:11:10 CDT 2006

Use the Exists method. Try something like

If Not persons("foo").Exists Then
persons.Add "foo", 1
Else
persons.Item("foo") = persons.Item("foo") + 1
End If


Steve


<redryderridesagain@hotmail.com> wrote in message
news:1149455679.823877.15430@y43g2000cwc.googlegroups.com...
> Using Scripting dictionary in Word, it seems that a retrieval of a
> non-existant item creates an item. In the code below the persons.count
> = 1 after the failed retrieval. How would one lookup an item and add
> one if not in the dictionary? Thanks.
>
> Sub dictionarytest()
> Dim c(2)
> Dim persons As Object
> Set persons = CreateObject("Scripting.Dictionary")
> Dim keys As Variant, dcount As Long
>
> On Error Resume Next
> ' This statement seems to add the item "foo" to the dictionary -
> why?
> dcount = persons("foo")(2)
> On Error GoTo 0
> dcount = persons.Count
> MsgBox ("count1 = " & dcount)
> c(1) = "foo"
> dcount = persons.Count + 1
> c(2) = dcount
> ' and it bombs here because "foo" is already in the dictionary
> persons.Add "foo", c
> End Sub
>



Re: Dictionary Objects - Looking up an item creates an item? by Steve

Steve
Sun Jun 04 17:38:35 CDT 2006

Once you build the scripting dictionary object as described in my post
above, use the keys method to return the collection of unique names (keys)
and you can then pull the count for each. For example if we stick with
'persons' as your dictionary object:

Dim myArray As Variant
myArray = persons.Keys
For i = 0 To persons.Count - 1
MsgBox myArray(i) & " was found " & persons.Item(myArray(i)) & " times."
Next i

Steve


<redryderridesagain@hotmail.com> wrote in message
news:1149455679.823877.15430@y43g2000cwc.googlegroups.com...
> Using Scripting dictionary in Word, it seems that a retrieval of a
> non-existant item creates an item. In the code below the persons.count
> = 1 after the failed retrieval. How would one lookup an item and add
> one if not in the dictionary? Thanks.
>
> Sub dictionarytest()
> Dim c(2)
> Dim persons As Object
> Set persons = CreateObject("Scripting.Dictionary")
> Dim keys As Variant, dcount As Long
>
> On Error Resume Next
> ' This statement seems to add the item "foo" to the dictionary -
> why?
> dcount = persons("foo")(2)
> On Error GoTo 0
> dcount = persons.Count
> MsgBox ("count1 = " & dcount)
> c(1) = "foo"
> dcount = persons.Count + 1
> c(2) = dcount
> ' and it bombs here because "foo" is already in the dictionary
> persons.Add "foo", c
> End Sub
>



Re: Dictionary Objects - Looking up an item creates an item? by Jezebel

Jezebel
Sun Jun 04 17:59:53 CDT 2006

Why not use a collection instead? Then a) you won't have this problem, and
b) you don't need any external libraries.



<redryderridesagain@hotmail.com> wrote in message
news:1149455679.823877.15430@y43g2000cwc.googlegroups.com...
> Using Scripting dictionary in Word, it seems that a retrieval of a
> non-existant item creates an item. In the code below the persons.count
> = 1 after the failed retrieval. How would one lookup an item and add
> one if not in the dictionary? Thanks.
>
> Sub dictionarytest()
> Dim c(2)
> Dim persons As Object
> Set persons = CreateObject("Scripting.Dictionary")
> Dim keys As Variant, dcount As Long
>
> On Error Resume Next
> ' This statement seems to add the item "foo" to the dictionary -
> why?
> dcount = persons("foo")(2)
> On Error GoTo 0
> dcount = persons.Count
> MsgBox ("count1 = " & dcount)
> c(1) = "foo"
> dcount = persons.Count + 1
> c(2) = dcount
> ' and it bombs here because "foo" is already in the dictionary
> persons.Add "foo", c
> End Sub
>



Re: Dictionary Objects - Looking up an item creates an item? by Steve

Steve
Mon Jun 05 00:14:22 CDT 2006

I find working with a collection more intuitive than scripting dictionary
but the scripting dictionary does let you easily check if a key exists
already, it lets you overwrite an existing member value easily and it's
performance is faster than working with a collection. I'm guessing that the
original poster is building a word count of some sort and the scripting
dictionary is well suited to that.

Steve


"Jezebel" <warcrimes@whitehouse.gov> wrote in message
news:OXxnXqCiGHA.1936@TK2MSFTNGP04.phx.gbl...
> Why not use a collection instead? Then a) you won't have this problem, and
> b) you don't need any external libraries.
>
>
>
> <redryderridesagain@hotmail.com> wrote in message
> news:1149455679.823877.15430@y43g2000cwc.googlegroups.com...
>> Using Scripting dictionary in Word, it seems that a retrieval of a
>> non-existant item creates an item. In the code below the persons.count
>> = 1 after the failed retrieval. How would one lookup an item and add
>> one if not in the dictionary? Thanks.
>>
>> Sub dictionarytest()
>> Dim c(2)
>> Dim persons As Object
>> Set persons = CreateObject("Scripting.Dictionary")
>> Dim keys As Variant, dcount As Long
>>
>> On Error Resume Next
>> ' This statement seems to add the item "foo" to the dictionary -
>> why?
>> dcount = persons("foo")(2)
>> On Error GoTo 0
>> dcount = persons.Count
>> MsgBox ("count1 = " & dcount)
>> c(1) = "foo"
>> dcount = persons.Count + 1
>> c(2) = dcount
>> ' and it bombs here because "foo" is already in the dictionary
>> persons.Add "foo", c
>> End Sub
>>
>
>



Re: Dictionary Objects - Looking up an item creates an item? by redryderridesagain

redryderridesagain
Mon Jun 05 23:55:12 CDT 2006

I'm happy to hear its faster and yes I'm using it to replace an, "add
unique strings to list --- well ---dictionary function".
As far as I can tell dictionaries (as implemented in VBA) have three
drawbacks;
- there is no dictionary of dictionaries
- some functions, such as the one in the original post do not follow
the law of least surprise
- I have not yet been able to update a single element of an array
dictionary item

I'm making do with a dictionary of collections

Steve Yandl wrote:
> I find working with a collection more intuitive than scripting dictionary
> but the scripting dictionary does let you easily check if a key exists
> already, it lets you overwrite an existing member value easily and it's
> performance is faster than working with a collection. I'm guessing that the
> original poster is building a word count of some sort and the scripting
> dictionary is well suited to that.
> >


Re: Dictionary Objects - Looking up an item creates an item? by John

John
Tue Jun 06 00:48:23 CDT 2006

On 5 Jun 2006 21:55:12 -0700, redryderridesagain@hotmail.com wrote:

>- some functions, such as the one in the original post do not follow
>the law of least surprise

This depends on the jurisdiction<g>. The VBS Dictionary object was
inspired by Perl's hash data type - and over there, the law of least
surprise requires that non-existent objects magically spring into being
when you reference them. So I guess that this behaviour was carried
across into VB territory.

--
John Nurick [Microsoft Access MVP]

Please respond in the newgroup and not by email.

Re: Dictionary Objects - Looking up an item creates an item? by redryderridesagain

redryderridesagain
Sat Jun 10 17:51:10 CDT 2006

To this list, I can add one additional drawback;
- dictionary lists are not sorted automatically by key

and take away one
- I saw an example of a dictionary of dictionaries

In sum, I'll stick with tables.

> As far as I can tell dictionaries (as implemented in VBA) have three
> drawbacks;
> - there is no dictionary of dictionaries
> - some functions, such as the one in the original post do not follow
> the law of least surprise
> - I have not yet been able to update a single element of an array
> dictionary item
>


Re: Dictionary Objects - Looking up an item creates an item? by John

John
Sun Jun 11 02:33:47 CDT 2006

On 10 Jun 2006 15:51:10 -0700, redryderridesagain@hotmail.com wrote:

>To this list, I can add one additional drawback;
>- dictionary lists are not sorted automatically by key

I don't know any language that does automatically sort dictionaries or
associative arrays. The problem (IMHO) is rather that VBScript and other
VB languages don't have a built-in function or method for sorting the
keys (unlike Perl, Python or JScript).

--
John Nurick [Microsoft Access MVP]

Please respond in the newgroup and not by email.