📄 22.html
字号:
<HTML><TITLE>Basic Syntax and I/O: Variables in Tcl</TITLE><BODY BGCOLOR="#FFF0E0" VLINK="#0FBD0F" TEXT="#101000" LINK="#0F0FDD">
<A NAME="top"><H1>Variables in Tcl</H1></A>
<P> In Tcl, variable names can be almost any string of characters. The one
restriction, explained below in
<A HREF="NotHere.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/NotHere.html">Regional Variables</A>, is that a variable name cannot
contain <TT>::</TT>. However, taking full advantage of Tcl's free form variable
names requires finesse and does not make for readable scripts. Accordingly, I
suggest you begin your variables names with a letter and continue them with
alphameric characters and the underscore character "_." (By
<CITE><NAME=#G2.2alphameric>alphameric</A></CITE> I mean a letter or a digit.)
<P> <STRONG>Remark</STRONG> <DL><DD> The restriction against including <TT>::</TT> in a variable
name was introduced with version 8.0. If you have an older script that
happens to have <TT>::</TT> in a variable name, the name will have to be changed.
</DL>
<P> Other than its name, the other important aspect of a variable is its value.
Variable values can also be arbitrary strings. In fact, they are always
either strings or one-dimensional arrays of strings – the only data types in
Tcl are strings and one-dimensional arrays. You never declare a Tcl
variable's type. The interpreter can infer that type from syntactical hints.
You must, however, assign values to variables before you use them – there are
no default initial values.
<P> When numbers are the values of variables, they have their ASCII form and not
one of the internal forms your computer uses for calculations.
<P> Unlike most programming languages, in Tcl, when you write the name of a
variable, you mean the name – not the value. That is why
<P> <PRE>
X + 1
</PRE>
would be an error if you meant "add one to <TT>X</TT>." The reason is that
"X" can only be the name of a variable, not its value. To represent
the value of a variable, put a dollar sign in front of it, for example, <TT>$X</TT>.
<P>
<P> Before each line of Tcl code is executed, something called
<CITE><NAME=#G2.2vrblsbstttn>variable substitution</A></CITE> happens: the line is searched for dollar
signs followed by variable names. Each such occurrence, for example, <TT>$X,</TT> is
replaced with the value of the named variable. Only after this substitution
process is complete is the command executed.
<P> Two caveats:
<UL>
<P> <P><LI> Variable substitution cannot happen if there is no value associated
with the variable – in that case you get an error.
<P> <P><LI> After a <TT>$,</TT> which characters are considered to be part of a
variable name? As many as possible – provided they are alphameric or an
underscore. If this is a problem, and it sometimes is, then use curly
brackets as shown in
<A HREF="2.10.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.10.html">More about Substitution</A>.
</UL>
<P> You already know that constructs like <TT>while</TT> which control the flow of
execution are implemented as procedures. You might guess that variable
assignment is too. You would be correct. The procedure is called
<TT><NAME=#Cset>set</A></TT> and here are examples of its use:
<PRE>
% set X 2
2
% set Y $X
2
</PRE>
As with many examples in this book, this one shows an interactive session.
Lines beginning with the <TT>%</TT> prompt are command lines. Other lines are
generated by Tcl in one of two ways: they show the return string from the
command on the previous line or they are the result of a <TT>puts</TT> command.
<P> In this example, the first statement assigns the value <TT>2</TT> to the
variable named <TT>X</TT> and returns the value. The second statement assigns the
value of the variable named <TT>X</TT> to the variable named <TT>Y</TT> and returns
the value. Of course, when both statements have executed, the value of both
variables is a string containing the numeric character <TT>2.</TT>
<P> By the way, Tcl handles return strings differently when executed
interactively than when executed directly. Interactive execution works as
shown with this example: return strings are printed in the command window.
With direct execution, return strings are normally ignored. The exception is
explained below in
<A HREF="2.7.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.7.html">Command Substitution</A>.
<P> <STRONG>Remark</STRONG> <DL><DD>
In this book, "a variable has a value <TT>V</TT>" and "a
variable contains <TT>V</TT>" mean the same thing.
</DL>
<P> <P><A NAME="2.2a">
<STRONG>Exercise 2.2a</STRONG> </A><DL><DD>
What are the values of <TT>X,</TT> <TT>Y,</TT> and
<TT>Z</TT> after these statements have executed?
<PRE>
set X Y
set $X 2
set Z $X.TCL
</PRE>
<P>
<A HREF="2.11.html#Sol2.2a" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.11.html#Sol2.2a">Solution</A></DL>
<P> Even though the <TT>set</TT> procedure returns the value that it assigns, this return
value is <CITE>ignored</CITE> except when a command is executed in a context that
requires a return value.
<P> The <TT>set</TT> procedure can also be used with just one argument. In that
case, the argument is viewed as a variable name and the value of that variable
is returned. Thus, <TT>set X</TT> is similar to <TT>$X</TT> but they are
appropriate in somewhat different contexts.
<!-- Linkbar -->
<P><CENTER><FONT SIZE=2><NOBR>
<STRONG>From</STRONG>
<A HREF="javascript:if(confirm('http://www.mapfree.com/sbf/tcl/book/home.html \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.mapfree.com/sbf/tcl/book/home.html'" tppabs="http://www.mapfree.com/sbf/tcl/book/home.html">Tcl/Tk For Programmers</A><WBR>
<STRONG>Previous</STRONG>
<A HREF="2.1.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.1.html">section</A><WBR>
<STRONG>Next</STRONG>
<A HREF="2.3.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.3.html">section</A><WBR>
<STRONG>All</STRONG>
<A HREF="2.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.html">sections</A><WBR>
<STRONG>Author</STRONG>
<A HREF="javascript:if(confirm('http://www.mapfree.com/mp/jaz/home.html \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.mapfree.com/mp/jaz/home.html'" tppabs="http://www.mapfree.com/mp/jaz/home.html">J. A. Zimmer</A><WBR>
<STRONG>Copyright</STRONG>
<A HREF="copyright.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/copyright.html">Notice</A><WBR>
<P>
<I>Jun 17, 1998</I>
</NOBR></FONT></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -