:TITLE: Tcl Data Structures 101 - The list ;# ;# RCSID: $Header: /cvsroot/tcl/tcltutorial/original/Tcl14.lsn,v 1.1 2004/11/04 16:01:13 davidw Exp $ ;# Copyright (c) 1995 Clif Flynt ;# 9300 Fleming Rd. ;# Dexter, MI 48130 ;# clif@cflynt.com ;# See file "NOTICE" for licensing terms. ;# :LESSON_TEXT_START_LEVEL 0: The list is the basic data structure to Tcl. A list is simply an ordered collection of stuff; numbers, words, strings, etc. For example, a command in Tcl is just a list in which the first list entry is the name of a proc, and subsequent members of the list are the arguments to the proc.
Lists can be created in several ways:
llength
command.
An individual list member can be accessed with the lindex
command.
The brief description of these commands is:
The items in list can be iterated through using the foreach command.
foreach varname list body
Foreach will execute the body code one time for each
list item in list. On each pass, varname will contain the
value of the next list item.
:TEXT_END:
:LESSON_TEXT_START_LEVEL 1:
The list is the basic data structure to Tcl. A list is simply an ordered
collection of stuff; numbers, words, strings, etc. For instance, a command in Tcl is
just a list in which the first list entry is the name of a proc, and subsequent
members of the list are the arguments to the proc.
Lists can be created in several ways:
lindex
command.
The brief description of these commands is:
split "1234567" "36"]
would split into a list composed of {12 45 7}.
The items in list can be iterated through using the foreach command.
foreach varname list body
Foreach will execute the body code one time for each
list item in list. On each pass, varname will contain the
value of the next list item.
:TEXT_END: :LESSON_TEXT_START_LEVEL 2: The list is the basic data structure to Tcl. A list is simply an ordered collection of stuff; numbers, words, strings, etc. Each entry in a list is treated as a single unit, even though it may be composed of several words or even multiple lines of text. For example, a command in Tcl is a list in which the first list entry is the name of a command, and subsequent members of the list are the arguments to the command.
The entries in a list are separated by white space. If an entry has white space within it, then that entry is enclosed within braces to make a single entry. An empty list entry is denoted by a pair of braces with nothing between them ({}).
Lists can be created in several ways:
lindex
command.
The brief description of these commands is:
split "1234567" "36"]
would split into a list composed of {12 45 7}.
The items in list can be iterated through using the foreach command.
foreach varname list body
Foreach will execute the body code one time for each
list item in list. On each pass, varname will contain the
value of the next list item.
The foreach example code is the equivalent of:
for {set i 0} {$i < [llength $x]} {incr i} {
puts "[lindex $x $i] is item number $i in list x"
}
The foreach command runs about twice as fast as the
for in this example. This is a good reason to use the
foreach in place of a for loop when it is
appropriate.
Even though you can create a list by simply setting a variable to a string
of words, it is better to use the list command when you are
creating lists because this will ensure that the items are grouped the
way you expect them to be grouped.
:TEXT_END:
:CODE_START:
set x "a b c"
puts "Item 2 of the list {$x} is: [lindex $x 2]\n"
set y [split 7/4/1776 "/"]
puts "We celebrate on the [lindex $y 1]'th day of the [lindex $y 0]'th month\n"
set z [list puts "arg 2 is $y" ]
puts "A command resembles: $z\n"
set i 0;
foreach j $x {
puts "$j is item number $i in list x"
incr i;
}
:TEXT_END: