In this article we will focus on the area of a different data structure available in TCL. Initially we will brief about three types of data structures and later will go in deeper for each of them. The three data structures that are available in TCL. The first one is list, the second one is called associative array, and the third one which is the newest and latest is called dictionary. You can see these three in the below figure.
List in TCL :
A list is the data-structure similar to the array in C-Language. The difference is that a list can contain any of the integer, string, float etc as a element. The different ways a list can be created are as follows :
set myList [list a b c]
set myList "a b c"
set myList {a b c}
set myList [list $a $b $c]
set myList {$a $b $c}
Anyone of the above notation will be accepted as creation of a valid list.
Length of a list :
length $myList
The output should be stored in a variable.
The length of the list can be calculated using the procedure length as :
Joining of More Than One List :
To join more elements we can use append procedure for an existing lista delimited string can be splitted into a list by using split procedure as :
split <delimited-string> <delimeter>
Lists can be joined together using concat procedure as :
concat <list1> <list2> ..... <listn>
To understand you may copy and run the below example. Also you may follow the narration mentioned in the video down below.
Sorting Inside a List :
We can sort the content of a list using the losrt procedure. Its different argument switch will allow us in the below way:
To sort in asciii order we use : -ascii
To sort in integer increasing order we use : -integer -increasing
To sort in integer decreasing order we use : -integer -decreasing
To sort in dictionary order we use : -dictionary
To sort in real increasing order we use : -real -increasing
To sort in real decreasing order we use : -real -decreasing
all of the above-mentioned sorting is exemplified by this code:
Iterating Over A List :
The best way to iterating over a list is to use the foreach loop. The foreach loop is already explained and exemplified in previous article. However run the below example to understand the iteration with a counter :
Append, Insert and Replace Elements Inside a List :
We can use lindex proc to get the positional value of a list element by its index.
lindex <list> <index>
Gives the value at the <index> of the <list>
To make a entry in the List from the R.H.S , we can use the below syntax :
lappend <list> <element>
To make a entry in the List from the L.H.S , we can use the below syntax :
linsert <list> 0 <element>
We can replace a range of elements in the the list by this syntax :
lreplace <list> <start-index> <end-index> <new-element-to-be-inserted>
All the above mentioned cases can be observed through the below code-snippet :
List of List :
Lists can be nested in the below way :
list a b {c d e} {f {g h}}
now {g h} is nested inside {f {g h}} which is further nested in the topmost list using list proc.
We can use split/concat/linsert/lreplace on list of list operations as shown in below code:
The below self-explanatory code example shows the way to do the hierarchical printing of list of list. For detailed explanation please watch the video down below in this page.
lsearch can be used to search any list element inside a existing list. This is explained through the below code example :
For detailed explanation please watch the video below :