List is a collection of data in the ordered format. Data can be of any format string, number, words etc. A list can be subset of another list. Multiple lists can be a part of the list.
Lets learn how we can create the lists using TCL built-in function list.list - creates a list
Syntax: list ?arg arg ...?Ex: set My_list [list a b "c d e " " f {g h}"]Now the list My_list contains a b {c d e } { f {g h}} as its elements.
lappend - appends list elements to a variable
Syntax: lappend variable_name ? value value value ...?Ex: lappend a 1 2 3The variable a contains 1 2 3 as list elements.
lassign - assigns list elements to variables
Syntax: lassign list variable_name ? var_name ...?Ex: lassign {i j k l} x y ; # Returns "k l"puts $x ; # Prints "i" on the consoleputs $y ; # Prints "j" on the consoleThe lassign commands assigns list elements to the variables following it.
lindex - used to extract an element from a list
Syntax: lindex list ?index...?Ex: lindex $a 0The lindex command extracts the element from the list at the index value. The indexing of list starts from "0".
linsert - inserts the elements into a list
Syntax: linsert list index element ? element element ...?Ex: set a { 1 2 3 4 }
linsert $a 2 5 6
Now the list contains 1 2 5 6 3 4 as its elements.At the index value of 2 the elements 5 6 are inserted into the list.
llength - gives the number of elements in a list
Syntax: llength listEx: llength $My_listThe output is 4 as list My_list contains four elements.
lrange - returns one or more adjacent elements from a list
Syntax: lrange list first lastEx: set a { 1 3 5 7 }lrange $a 2 endlrange is used to extract one or more than one adjacent elements from a list.The above example returns 5 7 as the output.
lreplace - replaces elements in a list with new elements
Syntax: lreplace list first last ?element element ...?Ex: set a {1 3 5 7}lreplace $a 1 1 9The element 3 is replaced by 9 in the above list. Now the new list contains 1 9 5 7 as its elements.
lrepeat - builds a list by repeating elements
Syntax: lrepeat number element1 ?element2 element3 ...?Ex: lrepeat 3 a Now the new list contains a a a as its elements.
lreverse - reverses the order of a list
Syntax: lreverse listEx: lreverse {a a b c}Now the new list contains c b a a as its elements.
lsearch - used to extract a particular element from a list
Syntax: lsearch ?options? list patternEx: lsearch {a b c d e} cThe output of above command is "2", it gives the index value of searched element.lsearch -all {a b c a b c} cIf the elements in a list are repeated to extract all the index values the switch -all should be used.The above command gives 2 5 as the index values of searched element c.
lset - used to change the element in a list
Syntax: lset varName ?index...? newValueEX: set s { a b c d }lset $s 2 fNow the list s contains a b f d as its elements.At the index value of 2, c is replaced by f.lset $s end hNow the new list contains a b c h as its elements.
lsort - sorts the elements of a list
Syntax: lsort ?options? ListEx: lsort { 9 7 6 3 }lsort command sorts the elements of a list.
concat - joins the lists together
Syntax: concat ?arg arg ...?Ex: set a {1 2 3} set b {4 5 6} concat $a $b 7Now the new list after concatenation contains 1 2 3 4 5 6 7 as its elements.
split - splits a string into a proper list
Syntax: split string ? splitcharacters?Ex: split "Day 2" {}Output: D a y { } 2The above example splits the elements with a null character.Null character is denoted by {} in the syntax.After splitting, the new list contains D a y { } 2 as its elements.Space is denoted by { } in the new list.split "D/a/y/2" / Output: D a y 2The above example splits the list by "/" character.
join - creates a string by joining together the elements of a list
Syntax: join list ? join_string? Ex: join { 2 4 6 8 } /Output: 2/4/6/8 The above example converts list elements into a string. Now the string contains 2/4/6/8 as its single element. join { 3 5 6 } ,Output: 3,5,6Now the elements of list are joined by "," character.