12/28/2021

Data Structures in TCL- Part-2 : Associative Array & Dictionary


In this article we are going to cover the Associative Array and Dictionary Data Structure in TCL. These two are marked in green in the below diagram.  In the previous article we have covered the Basic Array i.e List in TCL. 


Associative Array (HASH-Table):

An associative array is a look up  table where we can do one to one mapping of the key and value pairs. In this mapping the the key values are unique from each other however the value fields can be same for two different values.  Generally we do the one is to one mapping here. The key and the value pair could be of any type of data.  One important thing about the associative array is that it do not have the the index values represented by integers. Rather the index values could be anything among string , integer,  real number  or  alpha-numerical combination.

Here goes the first example to understand how to construct your first associative array.


set color(rose) red
set color(sky) blue
set color(medal) gold
set color(leaves) green
set color(blackboard) black


Here  the  name  associative array is "colour". The keys are constituted of Strings as rose, sky, medal,leaves and blackboard.The corresponding values are red,blue, gold, green and black. In this Array the key value pairs are made in 1:1 manner. To access the value of key-value pair from the constructed associative array, we have to use a foreach loop in the following manner :



The Output looks like :




"Array Set" and "Array Get" Method for Construction and Access of the Associative Array:


There is an alternative method to create an associative array. This is called the array set method. In this method we use one single procedure (named array) to write all the key value pairs in a single go. Here is an example for you :


array set colorobject {

rose red

sky blue

medal gold

leaves green

board black

}


This array set method produces the same result as the the previously shown method. We have a similar procedure to get the Key Value pairs from the associative array. This is called the Array get method. We can combine these two in the below code as :



The Output looks like :





Checking whether a HASH-Key exist :

When you have to access the key-value pair , you have to be over sanguine that the key is present. Only then you will be able to access the  key-value pair from the HASH-table structure. To do this we will use the info exist command on the key  , take its return value in a if check block through which we decide any further action on this key & corresponding value. 

Let us better understand with the below example : 



The Output looks like :





Associative Array to List conversion & Vise-versa :

We can use the above mentioned Array-Get & Array-Set methods to convert a Associative Array to List & the opposite too. If we pipe the output of Array-Get to any variable to store it , the outcome becomes a list as : 


set h2l [array get colorcount ]


To do the reverse i.e , convert a List into An Associative Array ,  we do the following :


array set l2h $h2l 


One condition applies here ,  the elements of the List has to be even numbers to form the key-value pair.

Now , let us try the whole thing through below code :


 


The output looks like this :






Dictionaries in TCL :

In TCL 8.5 the dict command has been introduced. This provides efficient access to key-value pairs. Dictionary is like associative arrays, but order of sequence is not important. Dictionaries can be passed on to a procedure. Unlike arrays, dictionaries can nest dictionaries. Hence Dictionary help you to  build complicated data structures, such as hierarchical databases.  You can also combine dictionaries with other TCL data structures.


Creating of Dictionary : 

The dictionary is created using the "dict create" method on even number of paired key-value combination as :

set FT [ dict create .txt Text .jpg Image .zip ZipArchive .doc Document ]


Here every odd number entry treated as key and the even numbered entry is treated as value.

We can add key-value pair even after this statement as :


dict set FT .mp3 Music 

dict set FT .avi Video


Special For Loop for Dictionaries ONLY :

TCL has "dict for" dedicated for the dictionaries ONLY.  Let us understand by the below example :



The output will look like this :




In this example the foreach loop is used to compare its output with the dict-for loop ! You can compare the two outputs from the snapshot above.


Checking whether a DICT-Key exist :

This checking is made simplified as compared to the associative array . "dict exist" method will return a positive value if the key really exist within the dictionary. You can understand this by the below code which is the continuation of the previous dictionary code mentioned just above.



The output looks like this :



Creating Complex Data Structure using Dictionary :


Beyond the paired key-value concept , the dictionary allows to create multiple attributed to a key. This is how :


dict set employeeInfo 12345-A forenames "Joe"

dict set employeeInfo 12345-A surname   "Schmoe"

dict set employeeInfo 12345-A street "147 Short Street"


Here the key "12345-A" has three attributes against it "forenames" , "surname" and "street". We can increase the number of attributes beyond shown above. Here the dictionary name is "employeeInfo" which is created to store the employee database information in the example.


Here is the full example :



This data structure can be read and printed using the below code :



To iterate we have used the special for loop designed for dictionary only. The output of the above code looks like this. 





To go through the explanatory video tutorial of this article , please watch the  below videos :







Courtesy : Image by www.pngegg.com