Tcl list : creation, length, and extracting element

In Tcl everything is a string. List is also a string. The simplest form of list is a string with zero or more space separated words. Lets set a variable with 3 elements list containing 1,2, and 3.

% set var1 “1 2 3”

% set var2 [list 1 2 3]

% set var3 {1 2 3}

Here we set var1,var2, and var3 to a list, with 3 different way.

To return the number of elements in a list we can use llength command.

% llength $var1

3

To extract an element from list we can use lindex command.

% lindex $var1   1

2

As we can see list has zero based index. The first element is at index 0. Lindex command can accept end and end-? as argument.

% lindex $var1   end

3

% lindex $var1  end-1

2

Thats all for now.

Leave a Reply

Your email address will not be published. Required fields are marked *