- Tcl - Regular Expressions
- Tcl - Built-in Functions
- Tcl - Error Handling
- Tcl - File I/O
- Tcl - Namespaces
- Tcl - Packages
- Tcl - Procedures
- Tcl - Dictionary
- Tcl - Lists
- Tcl - Strings
- Tcl - Arrays
- Tcl - Loops
- Tcl - Decisions
- Tcl - Operators
- Tcl - Variables
- Tcl - Data Types
- Tcl - Commands
- Tcl - Basic Syntax
- Tcl - Special Variables
- Tcl - Environment Setup
- Tcl - Overview
- Tcl - Home
Tk Tutorial
- Tk - Geometry Manager
- Tk - Windows Manager
- Tk - Events
- Tk - Images
- Tk - Fonts
- Tk - Mega Widgets
- Tk - Canvas Widgets
- Tk - Selection Widgets
- Tk - Layout Widgets
- Tk - Basic Widgets
- Tk - Widgets Overview
- Tk - Special Variables
- Tk - Environment
- Tk - Overview
Tcl/Tk Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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 pst. Further, a pst can contain another pst.
An important thing that needs to be noted is that these psts are represented as strings completely and processed to form inspanidual items when required. So, avoid large psts and in such cases; use array.
Creating a List
The general syntax for pst is given below −
set pstName { item1 item2 item3 .. itemn } # or set pstName [pst item1 item2 item3] # or set pstName [sppt "items separated by a character" sppt_character]
Some examples are given below −
#!/usr/bin/tclsh set colorList1 {red green blue} set colorList2 [pst red green blue] set colorList3 [sppt "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 pst is given below −
append pstName sppt_character value # or lappend pstName 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 pst is given below −
llength pstName
Example for length of pst 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 pst item at specific index is given below −
pndex pstname index
Example for pst item at index is given below −
#!/usr/bin/tclsh set var {orange blue red green} puts [pndex $var 1]
When the above code is executed, it produces the following result −
blue
Insert Item at Index
The syntax for inserting pst items at specific index is given below.
pnsert pstname index value1 value2..valuen
Example for inserting pst item at specific index is given below.
#!/usr/bin/tclsh set var {orange blue red green} set var [pnsert $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 pst items at specific indices is given below −
lreplace pstname firstindex lastindex value1 value2..valuen
Example for replacing pst 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 pst item at specific index is given below −
lset pstname index value
Example for setting pst 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 pstname variable1 variable2.. variablen
Example for transforming pst 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 pst is given below −
lsort pstname
An example for sorting a pst 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 redAdvertisements