Tcl - Lists



List is one of the basic data-type available in Tcl. It is used for representing an ordered collection of items. It can include different types of items in the same list. Further, a list can contain another list.

An important thing that needs to be noted is that these lists are represented as strings completely and processed to form individual items when required. So, avoid large lists and in such cases; use array.

Creating a List

The general syntax for list is given below −

set listName { item1 item2 item3 .. itemn }
# or
set listName [list item1 item2 item3]
# or 
set listName [split "items separated by a character" split_character]

Some examples are given below −

#!/usr/bin/tclsh

set colorList1 {red green blue}
set colorList2 [list red green blue]
set colorList3 [split "red_green_blue" _]
puts $colorList1
puts $colorList2
puts $colorList3

When the above code is executed, it produces the following result −

red green blue
red green blue
red green blue

Appending Item to a List

The syntax for appending item to a list is given below −

append listName split_character value
# or
lappend listName value

Some examples are given below −

#!/usr/bin/tclsh

set var orange
append var " " "blue"
lappend var "red" 
lappend var "green" 
puts $var

When the above code is executed, it produces the following result −

orange blue red green

Length of List

The syntax for length of list is given below −

llength listName

Example for length of list is given below −

#!/usr/bin/tclsh

set var {orange blue red green}
puts [llength $var] 

When the above code is executed, it produces the following result −

4

List Item at Index

The syntax for selecting list item at specific index is given below −

lindex listname index

Example for list item at index is given below −

#!/usr/bin/tclsh

set var {orange blue red green}
puts [lindex $var  1]

When the above code is executed, it produces the following result −

blue

Insert Item at Index

The syntax for inserting list items at specific index is given below.

linsert listname index value1 value2..valuen

Example for inserting list item at specific index is given below.

#!/usr/bin/tclsh

set var {orange blue red green}
set var [linsert  $var 3 black white]
puts $var

When the above code is executed, it produces the following result −

orange blue red black white green

Replace Items at Indices

The syntax for replacing list items at specific indices is given below −

lreplace listname firstindex lastindex value1 value2..valuen

Example for replacing list items at specific indices is given below.

#!/usr/bin/tclsh

set var {orange blue red green}
set var [lreplace $var 2 3 black white]
puts $var

When the above code is executed, it produces the following result −

orange blue black white

Set Item at Index

The syntax for setting list item at specific index is given below −

lset listname index value 

Example for setting list item at specific index is given below −

#!/usr/bin/tclsh

set var {orange blue red green}
lset var 0 black 
puts $var

When the above code is executed, it produces the following result −

black blue red green

Transform List to Variables

The syntax for copying values to variables is given below −

lassign listname variable1 variable2.. variablen

Example for transforming list into variables is given below −

#!/usr/bin/tclsh

set var {orange blue red green}
lassign $var colour1 colour2
puts $colour1
puts $colour2

When the above code is executed, it produces the following result −

orange
blue

Sorting a List

The syntax for sorting a list is given below −

lsort listname

An example for sorting a list is given below −

#!/usr/bin/tclsh

set var {orange blue red green}
set var [lsort $var]
puts $var

When the above code is executed, it produces the following result −

blue green orange red
Advertisements