I am looking up keys in a Scripting.Dictionary
to make sure I add them (and their items) only once to the dictionary:
If MyDict.Exists (Key) Then ' first internal key-value lookup
Set Entry=MyDict.Item (Key) ' Second internal key->value lookup
else
Set Entry=New ItemType
MyDict.Add Key,Entry
End If
' Now I work on Entry...
Exists
looks up the key in the dictionary, and Item ()
does it, too. So i get two key lookups for one logical lookup operation. Isn't there a better way?
The dox for the Item
property say
"If key is not found when attempting to return an existing item, a new key is created and its corresponding item is left empty." (MSDN)
This is true, i.e. looking up a non-existant key obviously makes this key part of the dictionary, probably with associated item = empty.
But what is that good for? How could I use this to boil it down to one lookup operation? How can I set the empty item after creating the key during the Item ()
property call?
The key problem to me is the fact that VBScript forces us to use
Set
with objects, andEmpty
is not an object. One trick that I've used in the past to get around this is to use theArray
function to create a temporary placeholder for the value. Then I can check the array to see if the value is an object or not. Applied to your example:In this case, though, you haven't eliminated the double lookup, but moved it from the "existing entry" case to the "new entry" case.
This code and output:
shows:
Set d(name) = object
- d(name) will create the key slot "name" if necessary and the Set assignment will put the object into the corresponding value (overwriting Empty or the 'old' object ('pointer')).If you append some details about what you really want to achieve, I'm willing to add to this answer.
Added:
If you (logically) work on a list of keys with duplicates and must add new objects to the dictionary on the fly, you can't avoid the double lookup, because you need to check for existence (1.0) and assign (2.0) the (perhaps newly created and assigned(1.5)) object to your work variable (see /m:a or /m:b in my sample code). Other languages with statements that deliver a value may allow something like
and without VBScript's Set vs. Let abomination something like
would do the extra work for new elements only, but all that is a pipe dream.
If those double lookups really matter (which I doubt - can you give an argument or evidence?), then the overall design of your program does matter. For example: If you can unique-fy your work list, everything becomes simple (see /m:c in my sample). Admittedly, I still have no idea, whether such changes are possible for your specific task.
Code to experiment with:
sample output:
The timing difference is probably caused by the reduced output of the /m:c mode, but it emphasizes the importance of not doing something more often then necessary.