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/tclshWe create tree lists and print their contents to the console.
set l1 { 1 2 3 }
set l2 [list one two three]
set l3 [split "1.2.3.4" .]
puts $l1
puts $l2
puts $l3
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
list
command. 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.tclOutput of the createlists.tcl script.
1 2 3
one two three
1 2 3 4
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/tclshThe script defines a list of numbers. We perform some operations on the list with specific list commands.
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]
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.tclOutput.
7
3
5
2 3 4
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/tclshWe go through list elements with the
foreach item {1 2 3 4 5 6 7 8 9} {
puts $item
}
foreach
command. foreach item {1 2 3 4 5 6 7 8 9} {Each loop cycle the item variable has a value from the list of numbers.
puts $item
}
$ ./traverse1.tclOuput.
1
2
3
4
5
6
7
8
9
In the second example we will go through items of the days list using the while loop.
#!/usr/bin/tclshWe 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]
set n [llength $days]
set i 0
while {$i < $n} {
puts [lindex $days $i]
incr i
}
set days [list Monday Tuesday Wednesday Thursday \We create a list of days.
Friday Saturday Sunday]
set n [llength $days]The length of the list is determined with the
llength
command. set i 0The is a counter.
while {$i < $n} {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]
incr i
}
puts [lindex $days $i]The
lindex
returns a value from the list pointed to by the counter. incr iThe counter is increased.
$ ./traverse2.tclOutput.
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
List operations
Now we will have some other list commands.#!/usr/bin/tclshWe have a list of three numbers.
set nums {4 5 6}
puts $nums
lappend nums 7 8 9
puts $nums
puts [linsert $nums 0 1 2 3]
puts $nums
lappend nums 7 8 9The
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.tclThis is the output of the operations.tcl script.
4 5 6
4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
4 5 6 7 8 9
In the following example, we will concatenate lists, search for items and replace items in lists.
#!/usr/bin/tclshWe define two animal lists. We introduce three new commands.
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]
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.tclExample output.
lion eagle elephant dog cat giraffe tiger horse dolphin
1
lion eagle elephant buffalo crocodile giraffe tiger horse dolphin
Sorting items
In this section, we will show how we can sort items in Tcl lists.#!/usr/bin/tclshTo sort list elements, we can use the
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]
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 }We have two lists. In the first we have strings, in the second numbers.
set nums { 1 5 4 3 6 7 9 2 11 0 8 2 3 }
puts [lsort $names]The default sorting is the ascii sorting. The elements are sorted by their positions in the ascii table.
puts [lsort -ascii $names]
puts [lsort -integer -increasing $nums]We treat the values as integers and sort them in increasing and decreasing orders.
puts [lsort -integer -decreasing $nums]
puts [lsort -integer -unique $nums]We sort the elements of the list in a numerical context in increasing order. Duplicates will be removed.
$ ./sorting.tclOutput.
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
Nested lists
In Tcl there can be nested lists; list in other lists.#!/usr/bin/tclshThis is a simple example with nested lists in Tcl.
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]
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.tclOutput.
6
4
1
2
4
In this part of the Tcl tutorial, we covered Tcl lists.
0 comments:
Post a Comment