TCL - list (array or vector)

About

From a usage point of view, a list is a finite ordered collection of element as (a,b,…,z).

  • Every list has a length, which is an integer >=0
  • Every element of the list is identified by its index into the list, which is an integer >=0 and < the list length.

In Tcl, a list consists of a string whose items are separated by white space. For example, this is a list,

1 2 3 4 5

In other languages, a list is sometimes known as :

  • an array
  • or vector (one-dimensional arrays of values)

Tcl lists are not fixed-length once created, as is the case in some other languages.

How to

create a list?

You can assign a list direct to a variable with the curly brace charcater.

set MyList {1 2 3 4 5}

or you can construct it using the list command as:

set MyList [list A B C D E]

retrieve an element ?

To retrieve the element at a particular index one uses the lindex command.

lindex $MyList 1

will return the value 2 because the first element is indexed to zero.

append an element ?

Elements can be appended to a list stored in a variable using the lappend command. (Hence, Tcl lists are not fixed-length once created, as is the case in some other languages.)

computes the length of any list

The llength command computes the length of any list for you.

Documentation / Reference


Powered by ComboStrap