⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 25.html

📁 Tcl 语言的入门级图书
💻 HTML
字号:
<HTML><TITLE>Basic Syntax and I/O: Curly Brackets</TITLE><BODY BGCOLOR="#FFF0E0" VLINK="#0FBD0F" TEXT="#101000" LINK="#0F0FDD">
<A NAME="top"><H1>Curly Brackets</H1></A>


<P> Here is a <TT>while</TT> loop for copying a file line by line.  The condition for
continuing the loop is shown partially in pseudocode.

<PRE>
while {-1 != <CITE>the value obtained by executing</CITE> gets $InFile Line} {
  puts $OutFile $Line
}
</PRE>

<P>  Two questions come immediately to mind:

<OL>
<P> <P><LI> How can this work when it appears on three lines instead of one?
<P> <P><LI> How can this work if the variables are substituted before the
<TT>while</TT> procedure is executed?
</OL>

The answers to both questions lie in the way curly brackets are processed.

<P> <CITE>When curly brackets surround a command argument,</CITE> they cause the Tcl
interpreter to consider everything between the curly brackets as a sealed
string that does not need processing.  In particular, it needs no substitutions
and no consideration of end-of-line symbols.  More precisely, 

<UL>

<P> <P><LI> the interpreter forms everything inside these brackets into a string

<P> <P><LI> while forming the string, it does not do any substitutions or pay
attention to blanks or new lines.  (An exception appears below in the 

<A HREF="2.10.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.10.html">More about Substitution</A>.)

<P> <P><LI> when done, it throws away the curly brackets and passes the string
as an argument to the procedure

</UL>

<P>  This means that the <TT>while</TT> statement shown above looks like

<PRE>
while {<CITE>CONDITION</CITE>}  {<CITE>BODY</CITE>}
</PRE> 

and there is no variable substitution within the curly brackets.  What you
see for <CITE>CONDITION</CITE> and <CITE>BODY</CITE> is what the <TT>while</TT> procedure sees.  At
the beginning of each iteration, when <TT>while</TT> is evaluating the
<CITE>CONDITION</CITE> argument, it performs substitutions as a part of that
evaluation.  During each iteration, <TT>while</TT> performs substitutions to the
lines in <CITE>BODY</CITE>.  These substitutions are normal Tcl substitutions.  They
have been suppressed by the curly brackets and are performed by <TT>while</TT>
as it goes about its business.

<P> Returning to the file copying loop, the <TT>while</TT> procedure will execute this
line over and over:

<P> <PRE>
puts $OutFile $Line
</PRE> 

<CITE>Each execution</CITE> of the line begins with Tcl's normal substitutions &#150; the
same substitutions that were suppressed by the use of the curly brackets
around the second argument.  The curly brackets ensure that it is the strings
<TT>$Outfile</TT> and <TT>$Line</TT> that are available when the line is executed &#150; not the values these strings represent.  By delaying the substitution of these
variables, Tcl allows the values to change during execution of the <TT>while</TT>
loop.

<P> Here are some examples that make additional points about curly brackets.  As
you read these examples, be aware that in them <TT>puts</TT> has just <CITE>one</CITE>
argument.  This fact may not be apparent as the examples appear to you.

<UL>

<P> <P><LI> Curly brackets may be nested.  For example,

<PRE>
% puts {a{x}b}
a{x}b
</PRE>

<P> <P><LI> An argument is considered to be surrounded by curly brackets if, and
only if, it begins with a curly bracket.  In the next example, the curly
brackets do not surround the argument.  Therefore, they do not affect
the substitution of the variable named <TT>X.</TT>

<PRE>
% puts 1{$X}1
1{$X}1
</PRE> 

The next example shows an error.

<PRE>
% puts {X}1
extra characters after close-brace
</PRE>

The error message is <TT>puts</TT>' return string.  The error is that the
argument begins with a curly bracket but is not surrounded by curly brackets.
</UL>

<P>  <P><A NAME="2.5a">
<STRONG>Exercise 2.5a</STRONG> </A><DL><DD>
  Write a Tcl command that assigns the string
"Hello World" (without the quotes) to the variable named <TT>X.</TT>
<P>
<A HREF="2.11.html#Sol2.5a" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.11.html#Sol2.5a">Solution</A></DL>
 

<P> <P><A NAME="2.5b">
<STRONG>Exercise 2.5b</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 2
set Y {$X}
set Z VAR-{$X}
</PRE>


In the last <TT>set</TT> statement there are just two arguments even though your
browser may show more.  

<P>
<A HREF="2.11.html#Sol2.5b" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.11.html#Sol2.5b">Solution</A></DL>
 
<P>  
<!-- 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.4.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.4.html">section</A><WBR>
<STRONG>Next</STRONG>
<A HREF="2.6.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.6.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 + -