📄 210.html
字号:
<HTML><TITLE>Basic Syntax and I/O: More about Substitution</TITLE><BODY BGCOLOR="#FFF0E0" VLINK="#0FBD0F" TEXT="#101000" LINK="#0F0FDD">
<A NAME="top"><H1>More about Substitution</H1></A>
<P> Besides variable and command substitution, Tcl command lines undergo
<CITE><NAME=#G2.10bckslshsbstttn>backslash substitution</A></CITE>. With backslash substitution, a backslash
(<TT>\</TT>) and some following characters are replaced with something else. Which
following characters and which replacement characters depend on context.
Backslash substitution actually performs several services. With it you can:
<OL>
<P> <P><LI> Use characters without triggering their special Tcl meanings. If you
write a backslash before a dollar sign, for example, then the dollar sign is
no longer a signal to perform variable substitution.
<P> For example, <TT>\$X</TT>
means the two characters <TT>$</TT> and <TT>X</TT> rather than the character <TT>\</TT>
followed by the value of <TT>X.</TT>
<P> The character you put after the backslash when you need this case is
nonalphameric.
<P> <P><LI> Put unprintable characters into Tcl scripts. For example, <TT>\n</TT> is a
new line character (or characters) and <TT>\t</TT> is a tab character. There are
only a few such backslash codes.
<P> The character you put after the backslash when you need this case is
a letter.
<P> <P><LI> A more general way to include an unprintable character is to write a
backslash and follow it with one, two, or three digits. These digits are
taken to represent the position of the character in the ASCII sequence --
using this form, the number must be written in octal. Even printable
characters can be represented this way. For example, <TT>\44</TT> is the same
character as <TT>\$.</TT>
<P> The character you put after the backslash when you need this case is
a digit.
<P> <P><LI> Make multiple lines seem like one line to the Tcl interpreter. If you
write a backslash before a carriage return, then the next line will be
considered to be a continuation of the line you just ended with exactly
one space in between. You can indent the continuation line and Tcl will
still see just one white space. For example
<PRE>
puts "Hellow\
orld"
</PRE>
causes "Hellow orld" to be printed. This is Tcl's solution to a common
problem of how to enter long strings without messing up your indentation.
<P> This form of backslash substitution happens <CITE>before anything else.</CITE> So
the above example would work just as well if curly brackets were used instead
of quotes.
</OL>
<P> For a complete description of backslash substitution, variable substitution,
and command substitution, look up "Tcl" in your on-line manual. Three more
points are worth making here:
<UL>
<P> <P><LI> The sequences <TT>\{</TT> and <TT>\}</TT> cannot be used as
part of the curly brackets which surround an argument. This is a little
surprising for <TT>{...\}</TT> – you might have thought that
because backslash substitution is not happening, the backslash is not noticed.
It is.
<P> <P><LI> Substitution happens only once. A dollar sign generated with this
backslash substitution, <TT>\44,</TT> does not then generate variable
substitution. Square brackets obtained with variable substitution from a
string value do not then generate command line substitution. And so on.
<P> <P><LI> Although Tcl was oriented solely toward strings that consist of normal
ASCII characters, that is changing. With version 8.1 (which is experimental
at the time of writing), it will be possible to work with international
characters using the <CITE><NAME=#G2.10unicode>unicode</A></CITE> coding scheme. To support unicodes,
backslash substitution for <TT>\u</TT> is being implemented. The substitution
involves the next four characters which must make a hexadecimal number
representing the 16 bits of the unicode. Here is an example where a Danish
word is placed in a string.
<PRE>
% set X VR\u00D8VL
VRØVL
</PRE>
Do not try this with a version of Tcl earlier than 8.1. By the way, the word
is used by Danes to tease foreigners who cannot pronounce their language.
<P> There are unicoded characters for all the world's major languages. There
are also unicodes for such symbols as the smiley face. Major companies such
as Microsoft have signed on to use unicode. Codes with the 0 in the first two
bytes cover many European languages. The last two bytes of those codes are an
earlier 8-bit code known as <CITE><NAME=#G2.10iso88591>ISO-8859-1</A></CITE>. Read more about
unicode in the on-line manual under "Utf" and at
<A HREF="javascript:if(confirm('http://www.unicode.org/ \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.unicode.org/'" tppabs="http://www.unicode.org/">the unicode Web site.</A> The exact encoding used
by Tcl is UTF-8.
</UL>
<P> <P><A NAME="2.10a">
<STRONG>Exercise 2.10a</STRONG> </A><DL><DD>
What will be output by each of the following
<TT>puts</TT> statements?
<PRE>
set X Zip
puts "\""
puts {\"}
puts \$X
puts "[set X]{$X}!"
puts {[set X]"$X"}
puts "\
HI"
puts {\
HI}
puts "\44"
puts {\44}
puts one{\44}more\ \}
puts one{\44}more }
set X 1; while {$X} "puts \44X; set X 0"
</PRE>
<P><A HREF="2.11.html#Sol2.10a" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.11.html#Sol2.10a">Solution</A></DL>
<P> <P><A NAME="2.10b">
<STRONG>Exercise 2.10b</STRONG> </A><DL><DD>
This is about using Tcl under DOS/Windows, but even those
of you with Unix versions should be able to answer it. Suppose you want to
pass the pathname <TT>C:\DOSNAME</TT> to the <TT>source</TT> command. One way is to
replace the backslash with a slash and rely on a nice conversion routine that
the Windows version of Tcl provides for <TT>source</TT>.
<P> Give two more ways that will work and explain why
<PRE>
source C:\DOSNAME
</PRE>
does not work.
<P><A HREF="2.11.html#Sol2.10b" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.11.html#Sol2.10b">Solution</A></DL>
<A NAME="smfnlpnts">
<H3>Some Final Points</H3></A>
<P> Skip this on first reading, if you like.
<P> If you want substitution to occur where it would not otherwise occur, there
is a <TT><NAME=#Csubst>subst</A></TT> command to do it for you. You are not likely to need
it.
<P> Suppose you want to do variable substitution in a setting where the
character after the variable name seems to be part of the variable name. For
example, you want to write
<PRE>
puts $X1
</PRE>
to mean "print the contents of <TT>X</TT> followed by the character <TT>1</TT>".
One way to solve this problem is to do command substitution, that is,
<PRE>
puts [set X]1
</PRE>
Tcl provides a second solution that makes use of curly brackets.
For example,
<PRE>
puts ${X}1
</PRE>
<P> Both methods work with variable names that contain nonstandard
characters. For example,
<PRE>
% set {bad variable name} 1
1
% puts [set {bad variable name}]
1
% puts ${bad variable name}
1
</PRE>
<!-- 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.9.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.9.html">section</A><WBR>
<STRONG>Next</STRONG>
<A HREF="2.11.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.11.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>
Nov 24, 1998
</FONT></CENTER>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -