Understanding Tcl Lists

Lists in Tcl are fundamental data structures that allow you to manage ordered collections of items. They can include a variety of item types, even other lists. Tcl handles lists as strings and processes them to form individual elements as needed.

Creating a List

Here are different ways to create a list in Tcl:

set listName {item1 item2 item3}
set listName [list item1 item2 item3]
set listName [split "item1,item2,item3" ","]

Example:

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

Output:

red green blue
red green blue
red green blue

Appending to a List

To append an item to a list, you can use:

lappend listName value

Example:

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

Output:

orange blue red green

Getting the Length of a List

To find the number of items in a list, use:

llength listName

Example:

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

Output:

4

Accessing List Items by Index

To retrieve an item at a specific index from a list, use:

lindex listName index

Example:

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

Output:

blue

Inserting Items at a Specific Index

To insert items at a specific position in a list, use:

linsert listName index value1 value2 ...

Example:

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

Output:

orange blue red black white green

Replacing Items in a List

To replace items in a list between specific indices, use:

lreplace listName firstIndex lastIndex value1 value2 ...

Example:

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

Output:

orange blue black white

Extracting a Sublist

To extract a sublist from a list, use:

lrange listName firstIndex lastIndex

Example:

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

Output:

blue red green

Setting an Item at a Specific Index

To set the value of an item at a specific index, use:

lset listName index value

Example:

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

Output:

black blue red green

Assigning List Values to Variables

To assign values from a list to individual variables, use:

lassign listName var1 var2 ...

Example:

set var {orange blue red green}
lassign $var color1 color2
puts $color1
puts $color2

Output:

orange
blue

Sorting a List

To sort the items in a list, use:

lsort listName

Example:

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

Output:

blue green orange red

Searching for an Item in a List

To find the index of an item in a list, use:

lsearch listName value

Example:

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

Output:

2
-1

Exercise: Manage a Basket of Fruits

Write a Tcl script to manage a basket of fruits (list). Initially, the basket is empty, and you will fill it with various fruits. Then, perform the following operations:

© 2025 semihub.in. All rights reserved.