English 中文(简体)
Tcl - Lists
  • 时间:2024-12-22

Tcl - Lists


Previous Page Next Page  

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 red
Advertisements