:TITLE: Assigning values to variables
;#
;# RCSID: $Header: /cvsroot/tcl/tcltutorial/original/Tcl2.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:
Set is the assignment operator in Tcl.
Tcl stores data as strings, and only converts to numbers when necessary.
When referring to a variable by value, the name is preceded by a $. When referring to the variable by reference, the $ is left off.
The argument to the set command must be passed by reference,
so it is called with simply a variable name, and no $.
The puts command accepts data passed by value, so you call
puts with a $ preceding the variable name.
If varName consists only of alphanumeric characters, and no parenthese, it is a scalar variable.
If varName has the form varName(index), it is a member of an associative array.
Set returns the value of variable varName.
:LESSON_TEXT_START_LEVEL 1: Tcl stores data as strings, and only converts to numbers when necessary for calculations or comparisons.
The assignment operator in Tcl is set.
When set is called with two arguments, as in:
set fruit "Cauliflower";
it places the second argument ("Cauliflower") in the memory space
referenced by the first argument
(fruit). Set always
returns the contents of the variable named in the first argument. Thus,
when
set is called with two arguments, it places the second argument in
the memory space referenced by the first argument and then returns
the second argument.
The first argument to a set command can be either a single word, like fruit or pi, or it can be a member of an array. Arrays will be discussed in greater detail later, for the time being just remember that many data can be collected under a single variable name, and an individual datum can be accessed by its index within that array. Indexing into an array in Tcl is handled by putting the index within parentheses after the name of the variable.
Set can also be invoked with only one argument. When called with just one argument, it will return the contents of that argument.
Here's a summary of the set command. Examine it, then click on the Run Example button, and study the output while you continue reading this text.
If varName consists only of alphanumeric characters, and no parenthese, it is a scalar variable.
If varName has the form varName(index), it is a member of an associative array.
Set returns the value of the variable varName.
If you look at the example code, you'll notice that in the
set command the first argument is typed with only its
name, but in the puts statement the argument is preceeded with
a $.
This is because Tcl passes data to subroutines either by reference or by value. Commands that don't change the contents of a variable usually have their arguments passed by value. Commands that DO change the value of the data must have the data passed by reference.
Again, modify the example scripts a little, and then click on to the next lesson. :TEXT_END:
:LESSON_TEXT_START_LEVEL 2: In order to make a programming language useful, you need to have a way to store and access data in the program. To do this, you allocate space in the computer's memory and put the data you wish to work with in that space. The data can be anything from the mass of a neutron to the poem about the Jabberwock. The important thing is that it is placed in computer memory, and that there is a way to get at it when you want to.
In Tcl space is reserved for you whenever you put data into a new variable, and you access the data you put in that space by that variable name.
To create a variable and put some data into it, we use the
set command.
When set is called with two arguments, as in:
set fruit "Cauliflower";
it places the second argument ("Cauliflower") in the memory space
referenced by the first argument
(fruit). Set always
returns the contents of the variable named in the first argument. Thus,
when
set is called with two arguments, it places the second argument in
the memory space referenced by the first argument and then returns
the second argument.
The first argument to a set command can be either a single word, like fruit or pi, or it can be a member of an array. Arrays will be discussed in greater detail later, for the time being just remember that many data can be collected under a single variable name, and an individual datum can be accessed by its index within that array. Indexing into an array in Tcl is handled by putting the index within parentheses after the name of the variable.
Set can also be invoked with only one argument. When called with just one argument, it will return the contents of that argument.
Here is an extract from the man page to summarize the way set behaves.
If varName consists only of alphanumeric characters, and no parenthese, it is a scalar variable.
If varName has the form varName(index), it is a member of an associative array.
Set returns the value of the variable varName.
Click on the Run Example button, and study the output while you continue reading this text.
If you look at the example code, you'll notice that in the
set command the first argument is typed with only its
name, while in the puts statement the argument is preceeded with
a $.
Sometimes when you pass the contents of a variable to a subroutine, you want to allow the subroutine to modify the data if it needs to, but not affect the main copy. In this case, you put a $ in front of the variable when you call the subroutine. Doing this reserves a new space in memory and makes a copy of the contents of variable_name. The subroutine is called with this copy of the data, and can do whatever is necessary as it processes the data. When it is done, the original copy of the data is still intact. This is called "Pass by Value", since the value of the variable is passed to the subroutine.
Other times (such as the set command), you want the
contents of the variable changed. In this case, you want to pass a
reference to the location in memory where that variable's data is
stored, so that the command can modify that data in place. In Tcl this
is done by *NOT* putting a $ in front of the variable name. This is
called "Pass by Reference", since a reference to the location is
passed to the command.
So, with the set command you simply use the variable name,
so that the reference is passed to set. Set
places the data in the second argument in the location referenced by the
first argument.
However, with the puts command, we don't want any chance of
our original data being changed, so we call puts by value,
and give it a fresh copy of the data to output.
Again, modify the example scripts a little, and then click on to the next lesson. :TEXT_END:
:CODE_START: ;# Assign a string to variable X set X "This is a string" ;# Assign a number to variable Y set Y 1.24 ;# Display the contents of X and Y puts $X puts $Y ;# Just puts a divider puts "..............................." ;# More than one item can be put in a single puts set label "The value in Y is: " puts "$label $Y" :TEXT_END: