Home SEO Tools HTML Escape Tool Character Counter Guest post

Lists in Tcl programming

0 comments


Tcl lists

In this part of the Tcl tutorial, we will talk about lists.
Computer programs work with data. Spreadsheets, text editors, calculators or chat clients. Working with groups of data is a basic programming operation. In Tcl, the listis a basic data structure. It is an ordered collection of items. Items in lists are separated by white space.
Every item of the list is identified by its index. Lists do not have a fixed length. List elements can be strings, numbers, variables, files or other lists. We can nest lists into other lists to any depth.

Creating lists

There are several ways, how we can create lists in Tcl.
#!/usr/bin/tclsh

set l1 { 1 2 3 }
set l2 [list one two three]
set l3 [split "1.2.3.4" .]

puts $l1
puts $l2
puts $l3
We create tree lists and print their contents to the console.
set l1 { 1 2 3 }
The basic way to create a list is to put elements of the list inside the brackets. List elements are separated by space.
set l2 [list one two three]
Another way to create a list is to use the listcommand.
set l3 [split "1.2.3.4" .]
Some Tcl commands return a list as a result. In the above code line, the split command returns a list of numbers generated from a string.
$ ./createlists.tcl
1 2 3
one two three
1 2 3 4
Output of the createlists.tcl script.

Basic list operations

In this section, we introduce some basic operations on lists. We will mention tree commands, that operate on Tcl lists.
#!/usr/bin/tclsh

set nums { 1 2 3 4 5 6 7 }

puts [llength $nums]
puts [lindex $nums 2]
puts [lindex $nums 4]
puts [lrange $nums 1 3]
The script defines a list of numbers. We perform some operations on the list with specific list commands.
puts [llength $nums]
The llength command returns a length of the list.
puts [lindex $nums 2]
The lindex command returns an item on the third position of the list. The positions in Tcl lists start from 0.
puts [lrange $nums 1 3]
The lrange command returns a subset of the list.
$ ./basicoperations.tcl
7
3
5
2 3 4
Output.

Traversing lists

Now that we have defined lists and basic list operations, we want to go traverse the list elements. We show several ways how to go through the list items.
#!/usr/bin/tclsh

foreach item {1 2 3 4 5 6 7 8 9} {

puts $item
}
We go through list elements with the foreachcommand.
foreach item {1 2 3 4 5 6 7 8 9} {

puts $item
}
Each loop cycle the item variable has a value from the list of numbers.
$ ./traverse1.tcl
1
2
3
4
5
6
7
8
9
Ouput.

In the second example we will go through items of the days list using the while loop.
#!/usr/bin/tclsh

set days [list Monday Tuesday Wednesday Thursday \
Friday Saturday Sunday]
set n [llength $days]

set i 0

while {$i < $n} {

puts [lindex $days $i]
incr i
}
We traverse the list using a while loop. When working with a while loop, we also need a counter and a number of items in the list.
set days [list Monday Tuesday Wednesday Thursday \
Friday Saturday Sunday]
We create a list of days.
set n [llength $days]
The length of the list is determined with the llengthcommand.
set i 0
The is a counter.
while {$i < $n} {

puts [lindex $days $i]
incr i
}
The while loop executes the commands in the body, until the counter is equal to the number of elements in the list.
puts [lindex $days $i]
The lindex returns a value from the list pointed to by the counter.
incr i
The counter is increased.
$ ./traverse2.tcl
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
Output.

List operations

Now we will have some other list commands.
#!/usr/bin/tclsh

set nums {4 5 6}
puts $nums

lappend nums 7 8 9
puts $nums

puts [linsert $nums 0 1 2 3]
puts $nums
We have a list of three numbers.
lappend nums 7 8 9
The lappend appends data to the list.
puts [linsert $nums 0 1 2 3]
The linsert inserts elements at a given index. The first number is the index. The remaining values are numbers to be inserted into the list. The command creates a new lists and returns it. It does not modify the original list.
$ ./operations.tcl 
4 5 6
4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
4 5 6 7 8 9
This is the output of the operations.tcl script.

In the following example, we will concatenate lists, search for items and replace items in lists.
#!/usr/bin/tclsh

set animals1 { lion eagle elephant dog cat }
set animals2 { giraffe tiger horse dolphin }

set animals [concat $animals1 $animals2]

puts $animals

puts [lsearch -exact $animals eagle]
puts [lreplace $animals 3 4 buffalo crocodile]
We define two animal lists. We introduce three new commands.
set animals [concat $animals1 $animals2]
The concat command is used to concatenate (add) two lists. The above line joins two lists and the new list is set to the animals variable.
puts [lsearch -exact $animals eagle]
With the lsearch command we look for an eagle in the list. With the -exact option we look for an exact match. The command returns the index of the first matching element. Or -1 if there is no match.
puts [lreplace $animals 3 4 buffalo crocodile]
The lreplace command replaces dog and cat with buffalo and crocodile.
$ ./operations2.tcl
lion eagle elephant dog cat giraffe tiger horse dolphin
1
lion eagle elephant buffalo crocodile giraffe tiger horse dolphin
Example output.

Sorting items

In this section, we will show how we can sort items in Tcl lists.
#!/usr/bin/tclsh

set names { John Mary Lenka Veronika Julia Robert }
set nums { 1 5 4 3 6 7 9 2 11 0 8 2 3 }

puts [lsort $names]
puts [lsort -ascii $names]
puts [lsort -ascii -decreasing $names]
puts [lsort -integer -increasing $nums]
puts [lsort -integer -decreasing $nums]
puts [lsort -integer -unique $nums]
To sort list elements, we can use the sort command. The command does not modify the original list. It returns a new sorted list of elements.
set names { John Mary Lenka Veronika Julia Robert }
set nums { 1 5 4 3 6 7 9 2 11 0 8 2 3 }
We have two lists. In the first we have strings, in the second numbers.
puts [lsort $names]
puts [lsort -ascii $names]
The default sorting is the ascii sorting. The elements are sorted by their positions in the ascii table.
puts [lsort -integer -increasing $nums]
puts [lsort -integer -decreasing $nums]
We treat the values as integers and sort them in increasing and decreasing orders.
puts [lsort -integer -unique $nums]
We sort the elements of the list in a numerical context in increasing order. Duplicates will be removed.
$ ./sorting.tcl
John Julia Lenka Mary Robert Veronika
John Julia Lenka Mary Robert Veronika
Veronika Robert Mary Lenka Julia John
0 1 2 2 3 3 4 5 6 7 8 9 11
11 9 8 7 6 5 4 3 3 2 2 1 0
0 1 2 3 4 5 6 7 8 9 11
Output.

Nested lists

In Tcl there can be nested lists; list in other lists.
#!/usr/bin/tclsh

set nums {1 2 {1 2 3 4} {{1 2} {3 4}} 3 4}

puts [llength $nums]
puts [llength [lindex $nums 2]]

puts [lindex $nums 0]
puts [lindex [lindex $nums 2] 1]
puts [lindex [lindex [lindex $nums 3] 1] 1]
This is a simple example with nested lists in Tcl.
set nums {1 2 {1 2 3 4} {{1 2} {3 4}} 3 4}
A list with two nested lists. The second list has two additional inner nested lists.
puts [llength $nums]
We determine the size of the list. The nested list is counted as one element.
puts [llength [lindex $nums 2]]
In this line, we determine the size of the first nested list, which is the third element of the main list.
puts [lindex $nums 0]
Here we print the first element of the main list.
puts [lindex [lindex $nums 2] 1]
In the above line, we get the second element of the first nested list.
puts [lindex [lindex [lindex $nums 3] 1] 1]
Here we get the second element of the second inner list of the inner list located at the 4th position of the main list. In other words: the inner most command is executed first. The [lindex $nums 3] returns {{1 2} {3 4}}. Now the second command operates on this returned list. [lindex {{1 2} {3 4}} 1] returns {3 4}. Finally, the last command [lindex {3 4} 1] returns 4, which is printed to the terminal.
$ ./nestedlists.tcl
6
4
1
2
4
Output.
In this part of the Tcl tutorial, we covered Tcl lists.

Do you like this Tutorial? Please link back to this article by copying one of the codes below.

URL: HTML link code: BB (forum) link code:

No comments:

FAQs | Privacy Policy | Contact | | Advertise | Donate