Dictionary Objects - Looking up an item creates an item?

  • Thread starter redryderridesagain
  • Start date
R

redryderridesagain

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
 
S

Steve Yandl

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
 
S

Steve Yandl

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
 
J

Jezebel

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

Steve Yandl

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
 
R

redryderridesagain

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
 
J

John Nurick

- 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.
 
R

redryderridesagain

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.
 
J

John Nurick

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top