Dictionary Object v/s Arrays:-
In simple terms, Dictionary object is similar to a typical array. The difference between a dictionary object and an array is that there is a unique key associated with every item of dictionary object. This unique key can help you in calling that item as and when required.
Declaring a Dictionary Object -
Dictionary is a COM object, it can be instantiated in the same way as any other COM Object.
Set dict = CreateObject("Scripting.Dictionary")
Adding keys and corresponding items -
Dictionary keys must be unique.
dict.Add "Company1", "IBM"
What are the places where it can be used?
When you want to share data between different actions in a test, dictionary object can be used. To do this you should create a reserved test object for the Dictionary object.
Why dictionary object and why not array only?
As shown in the example above, dictionary object was used with the index (key) being string. In the case of array, the index can be ONLY numeric. Then of course we have some obvious advantages like referencing a value with a meaningful keys etc.
OK, but what is it actually used for?
I was given a problem, not too long ago, that required me to compare two very large sets of data. They were both over 300,000 lines.
No problem, I said, give me a couple of minutes, and I will then give you the differences between the two lists. The data I needed to compare was a simple one, a key and a value. I was required to first report of any keys found in one list and not in the other, and if the key was there, I then had to make sure that the values were also the same.
I quickly created a multidimensional array for each list, populated the arrays, which took a while, but then adding 300,000 values into a multidimensional is not a quick process, and started the comparison. Needless to say, it didn’t return me any results – my machine died on me before any results were displayed.
I went back and said that it will take me a little longer, and that I needed more memory, when I remembered about the dictionary object. This method was not only quicker, but also easier (for me that is) to use, and I’m not talking about the MS Word dictionary here.
A dictionary holds two sets of information, a unique key and a value associated with the key, so we use the key to retrieve the item. The key can be anything except a variant, but it’s should usually be an integer or a string. The item can be of any type.
So, what are the methods that can be used with Dictionary Objects:-
The below code answers - ' How to iterate through the complete list of a Dictionary?'
Set dict = CreateObject("Scripting.Dictionary")
dict.Add "Company1", "TCS"
dict.Add "Company2", "IBM"
dict.Add "Company3", "Accenture"
itemD1 = dict.Items
keyD1 = dict.Keys
countD1 = dict.Count
Print "Key-Value Pair:"
For i = 0 To countD1-1
Print keyD1(i) & ": " & itemD1(i)
Next
Output:
Key-Value Pair
Company1: TCS
Company2: IBM
Company3: Accenture
In simple terms, Dictionary object is similar to a typical array. The difference between a dictionary object and an array is that there is a unique key associated with every item of dictionary object. This unique key can help you in calling that item as and when required.
Declaring a Dictionary Object -
Dictionary is a COM object, it can be instantiated in the same way as any other COM Object.
Set dict = CreateObject("Scripting.Dictionary")
Adding keys and corresponding items -
Dictionary keys must be unique.
dict.Add "Company1", "IBM"
What are the places where it can be used?
When you want to share data between different actions in a test, dictionary object can be used. To do this you should create a reserved test object for the Dictionary object.
Why dictionary object and why not array only?
As shown in the example above, dictionary object was used with the index (key) being string. In the case of array, the index can be ONLY numeric. Then of course we have some obvious advantages like referencing a value with a meaningful keys etc.
OK, but what is it actually used for?
I was given a problem, not too long ago, that required me to compare two very large sets of data. They were both over 300,000 lines.
No problem, I said, give me a couple of minutes, and I will then give you the differences between the two lists. The data I needed to compare was a simple one, a key and a value. I was required to first report of any keys found in one list and not in the other, and if the key was there, I then had to make sure that the values were also the same.
I quickly created a multidimensional array for each list, populated the arrays, which took a while, but then adding 300,000 values into a multidimensional is not a quick process, and started the comparison. Needless to say, it didn’t return me any results – my machine died on me before any results were displayed.
I went back and said that it will take me a little longer, and that I needed more memory, when I remembered about the dictionary object. This method was not only quicker, but also easier (for me that is) to use, and I’m not talking about the MS Word dictionary here.
A dictionary holds two sets of information, a unique key and a value associated with the key, so we use the key to retrieve the item. The key can be anything except a variant, but it’s should usually be an integer or a string. The item can be of any type.
So, what are the methods that can be used with Dictionary Objects:-
The below code answers - ' How to iterate through the complete list of a Dictionary?'
Set dict = CreateObject("Scripting.Dictionary")
dict.Add "Company1", "TCS"
dict.Add "Company2", "IBM"
dict.Add "Company3", "Accenture"
itemD1 = dict.Items
keyD1 = dict.Keys
countD1 = dict.Count
Print "Key-Value Pair:"
For i = 0 To countD1-1
Print keyD1(i) & ": " & itemD1(i)
Next
Output:
Key-Value Pair
Company1: TCS
Company2: IBM
Company3: Accenture
Questions that can be explored further, for fun:-
- How do we pass dictionary objects between actions?
- When to use CompareMode Property of Dictionary Objects?
Next Post: UFT Code#2: To execute UFT from a .vbs file
====================*************************************====================