Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
{section} {column:width=70%} h2. Introduction This page reviews some basic aspects of Tcl that are central in QTcl programming. In particular the difference between immediate and delayed evaluation of variables is discussed. To learn more about Tcl, access the [Tcl documentation|http://www.tcl.tk/doc/] or the [Tcl tutorial section|http://www.tcl.tk/man/tcl8.5/tutorial/tcltutorial.html] {column} {column:width=30%} \\ \\ *In this section:* {toc} {column} {section} h2. Strings and Lists Strings in Tcl are simply concatanated characters. If the string contains whitespace it must be delimited by quotes: *Tcl Strings* {noformat:|borderWidth=1|bgColor=#eeeeee}
Wiki Markup
Section
Column
width70%

Introduction

This page reviews some basic aspects of Tcl that are central in QTcl programming. In particular the difference between immediate and delayed evaluation of variables is discussed.

To learn more about Tcl, access the Tcl documentation or the Tcl tutorial section

Column
width30%



In this section:

Table of Contents

Strings and Lists

Strings in Tcl are simply concatenated characters. If the string contains white-space it must be delimited by quotes:

Tcl Strings

No Format
bgColor#eeeeee
borderWidth1
# Legal string syntax in Tcl
set x hello
set y "hello"
set z "hello world"
{noformat}

If

...

the

...

string

...

contains

...

variables

...

these

...

are

...

resolved

...

immediately:

...

Variables

...

Resolved

...

in

...

a

...

String

...

No Format
bgColor#eeeeee
borderWidth1
set x 4
puts "He ate $x apples" ; # prints 'He ate 4 apples'
{noformat}

Lists

...

are

...

elements

...

delimited

...

by

...

'

...

{...}'

...

or

...

created

...

using

...

the

...

list

...

command.

...

The

...

elements

...

may

...

be

...

any

...

variable

...

or

...

data

...

structure

...

allowed

...

in

...

Tcl,

...

including

...

other

...

lists.

...

Creating

...

Lists

...

No Format
bgColor#eeeeee
borderWidth1
set l { He ate 4 "green apples" }
set l [list He ate 4 "green apples" ]
{noformat}

This

...

lists

...

contains

...

4

...

elements,

...

the

...

last

...

element

...

being

...

"green

...

apples."

...

Evaluation

...

of

...

Variables

...

Lists

...

created

...

with

...

braces

...

differ

...

from

...

strings

...

in

...

that

...

variables

...

are

...

not

...

resolved

...

when

...

the

...

list

...

is

...

declared.

...

Simple

...

List

...

No Format
bgColor#eeeeee
borderWidth1
set x 4
puts "He ate $x apples"   ; # prints 'He ate 4 apples'
puts { He ate $x apples } ; # prints 'He ate $x apples'
{noformat}

The

...

use

...

of

...

braces

...

to

...

preserve

...

variables

...

is

...

useful

...

when

...

statements

...

are

...

to

...

be

...

evaluated

...

at

...

a

...

later

...

point

...

of

...

execution.

...

This

...

mode

...

of

...

programming

...

is

...

greatly

...

used

...

when

...

creating

...

interface

...

menus,

...

when

...

so-called

...

callback

...

functions

...

are

...

assigned

...

to

...

various

...

elements

...

of

...

the

...

menu.

...

The

...

callback

...

functions

...

will

...

be

...

called

...

in

...

response

...

to

...

a

...

window

...

event,

...

which

...

may

...

happen

...

at

...

any

...

time.

...

Callback

...

Functions

...

No Format
bgColor#eeeeee
borderWidth1
set x 4
qpushbutton "Immediate" "qinformation Information $x" ; # displays '4'
qpushbutton "Delayed" { qinformation Information $x } ; # displays '5'
set x 5
{noformat}

In

...

current

...

example

...

showing

...

Callback

...

Functions

...

,

...

two

...

buttons

...

are

...

created

...

which

...

both

...

display

...

the

...

value

...

of

...

x

...

when

...

activated.

...

The

...

first

...

button

...

is

...

given

...

a

...

callback

...

string

...

which

...

immediately

...

resolves

...

to

...

'qinformation

...

4'.

...

This

...

string

...

is

...

stored

...

together

...

with

...

the

...

button

...

and

...

is

...

implemented

...

at

...

the

...

same

...

time

...

the

...

user

...

activates

...

the

...

button.

...

By

...

this

...

time

...

x

...

has

...

been

...

set

...

to

...

'5',

...

but

...

that

...

no

...

longer

...

affects

...

the

...

contents

...

of

...

the

...

callback

...

string.

...

The

...

second

...

button

...

is

...

given

...

a

...

brace-delimited

...

list

...

as

...

the

...

callback

...

function.

...

The

...

variable

...

x

...

is

...

not

...

resolved

...

and

...

the

...

list

...

{

...

qinformation

...

$x

...

}

...

is

...

assigned

...

as

...

callback

...

function

...

regardless

...

of

...

the

...

value

...

of

...

x.

...

When

...

the

...

user

...

later

...

activates

...

the

...

button

...

however,

...

x

...

holds

...

the

...

value

...

'5',

...

and

...

this

...

is

...

displayed

...

on

...

the

...

output

...

channel.

...

Command

...

Substitution

...

A

...

special

...

form

...

of

...

command

...

evaluation

...

involves

...

the

...

square

...

brackets

...

'

...

[ref:

...

...

...

]'.

...

When

...

encountering

...

an

...

expression

...

in

...

square

...

brackets,

...

Tcl

...

will

...

evaluate

...

the

...

expression

...

inside,

...

and

...

replace

...

the

...

expression

...

in

...

brackets

...

with

...

the

...

result

...

before

...

evaluating

...

the

...

outer

...

statement.

...

Command

...

Substitution

...

No Format
bgColor#eeeeee
borderWidth1
set result [expr 2 + 2 ]
puts $result

{noformat}

In

...

the

...

above

...

example

...

the

...

command

...

'expr

...

2

...

+

...

2'

...

is

...

evaluated

...

and

...

replaced

...

with

...

it's

...

result,

...

which

...

is

...

'4'.

...

Any

...

valid

...

Tcl

...

statement

...

that

...

returns

...

a

...

value

...

can

...

be

...

placed

...

inside

...

square

...

brackets.

...

The

...

list

...

command,

...

for

...

example,

...

returns

...

a

...

new

...

list

...

formed

...

by

...

concatenating

...

the

...

elements

...

provided

...

as

...

arguments

...

:

...

Nested

...

List

...

Expression

...

No Format
bgColor#eeeeee
borderWidth1
set l [list 1 2 3]

{noformat}

Here

...

the

...

variable

...

'l'

...

is

...

assigned

...

and

...

the

...

value

...

returned

...

by

...

the

...

bracketed

...

expression,

...

which

...

in

...

this

...

case

...

creates

...

a

...

list.

...