0% found this document useful (0 votes)
7 views19 pages

TCL Lists

The document provides an overview of lists in Tcl, explaining their structure as ordered collections of elements that can include strings, numbers, and other lists. It details various commands for creating, manipulating, and querying lists, such as appending items, extracting elements, and sorting. Additionally, it introduces the 'split' and 'join' commands for breaking strings into lists and concatenating lists into strings, respectively.

Uploaded by

abinava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views19 pages

TCL Lists

The document provides an overview of lists in Tcl, explaining their structure as ordered collections of elements that can include strings, numbers, and other lists. It details various commands for creating, manipulating, and querying lists, such as appending items, extracting elements, and sorting. Additionally, it introduces the 'split' and 'join' commands for breaking strings into lists and concatenating lists into strings, respectively.

Uploaded by

abinava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

TCL – Lists

Dr. C. Prayline Rajabai


Assistant Professor Senior
School of Electronics Engineering
Vellore Institute of Technology
Lists
 Lists are used in Tcl to deal with collections of things.

 A list is an ordered collection of elements where each element can have any string value such as a

number, a person’s name, a word of a Tcl command..

 A list can contain another list.

 Lists are used in Tcl to deal with collections of things, such as all the users in a group or all the files in a

directory

 Lists are represented as strings and processed to form individual items when required.

2 VIT - SENSE 09-01-2024


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]

3 VIT - SENSE 09-01-2024


Examples
set colorList1 {red green blue}

set colorList2 [list red green blue]

set colorList3 [split "red_green_blue" _]

puts $colorList1

puts $colorList2

puts $colorList3

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

red green blue

red green blue

red green blue

4 VIT - SENSE 09-01-2024


Creating a list – concat, list
 Tcl provides two commands that combine strings together to produce lists

 concat and

 list

 These commands accepts an arbitrary number of arguments

 These commands produces a list as a result. Note the resultant list differs based on the
command used.
concat {a b c} {d e} f {g h i}
⇒abcdef ghi

list {a b c} {d e} f {g h i}
⇒ {a b c} {d e} f {g h i}

5 VIT - SENSE 09-01-2024


Creating a list – concat, list
Example

set x {a b c}

set y {d e f}
set z [concat $x $y]

z ⇒ abcdef

set z “$x $y”

z ⇒ abcdef

set z [list $x $y]

z ⇒ {a b c} {d e f}

6 VIT - SENSE 09-01-2024


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
Examples :
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

7 VIT - SENSE 09-01-2024


Length of the list
 The syntax to get the length of the 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.

8 VIT - SENSE 09-01-2024


Select an item from the list
 The syntax for selecting an item from the list is given below.

lindex listname index

Example for selecting an item from the list is given below.

#!/usr/bin/tclsh

set var {orange blue red green}

puts [lindex $var 1]

When above code is executed, it produces following result.

blue

9 VIT - SENSE 09-01-2024


Extracting elements from a list
 The syntax for extracting the list items at specific indices is given below.

lrange listname firstindex lastindex

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

#!/usr/bin/tclsh

set var {orange blue red green}

puts [lrange $var 1 3]

When above code is executed, it produces following result.

blue red green

10 VIT - SENSE 09-01-2024


Insert elements to a list
 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.

set var {orange blue red green}

set var [linsert $var 3 black white]

puts $var

When above code is executed, it produces following result.

orange blue red black white green

11 VIT - SENSE 09-01-2024


Replace/delete 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 of items at specific indices is given below.

set var {orange blue red green}

set var [lreplace $var 2 3 black white]

puts $var

When above code is executed, it produces following result.

orange blue black white

12 VIT - SENSE 09-01-2024


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.

set var {orange blue red green}

lset var 0 black

puts $var

When above code is executed, it produces following result.

black blue red green

13 VIT - SENSE 09-01-2024


Transform list to simple 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.

set var {orange blue red green}

lassign $var colour1 colour2

puts $colour1

puts $colour2

When above code is executed, it produces following result.

orange

blue
14 VIT - SENSE 09-01-2024
Sorting a list
 The syntax for sorting a list is given below.

lsort optional_switches listname

Example for sorting a list is given below.

set var {orange blue red green}

set var [lsort $var]

puts $var

When above code is executed, it produces following result.

blue green orange red

 Optional switches : -decreasing and –integer.

15 VIT - SENSE 09-01-2024


Searching an item from a list
 The syntax for searching an item from a list is given below.

lsearch list value

Example for searching an item from a list is given below.

set var {orange blue red green}

puts [lsearch $var red];

puts [lsearch $var yellow]

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

-1

16 VIT - SENSE 09-01-2024


“split” command
 Split command is used to break up a string into pieces

 First argument to the split is the string to be split up and the second argument is the split

character.

split $var_name split_character

Example for split operation

set x a/b/c

split $x /

=>a b c

set x /a/b/c

split $x /

=> { } a b c

17 VIT - SENSE 09-01-2024


“join” command
 Inverse of split command is the join command

 It concatenates the list elements together with a separator string between them.

 First argument to the join is the list to be joined up and the second argument is the join character.

split $list_name join_character

Example for join operation

join {{} a b c} /

 /a/b/c

set x {24 112 5}

expr [join $x +]

 141

18 VIT - SENSE 09-01-2024


Thank You…

You might also like