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

📄 27.html

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


<P> So far, you have seen that procedures and commands are invoked with lines of
the form,

<PRE>
command <CITE>ARG1 ARG2 ... ARGn</CITE>
</PRE>

and that their return values are thrown away.  To use 
the return value, invoke the command with this syntax:

<P> <PRE>
[command <CITE>ARG1 ARG2 ... ARGn</CITE>]
</PRE> 

within another command line.  This form is useful anyplace where the return
value will be useful.  The square brackets cause a form of substitution to
happen.  First, the command line inside the square brackets is executed.
Second, the result of that command is substituted in place of the square
brackets.  This process is called <CITE><NAME=#G2.7cmmndsbstttn>command substitution</A>.</CITE>

<P>  For example, when executing the command,

<PRE>
set X [set Y 0]
</PRE>

there will be a substitution phase for the overall command line.  During
this phase the command line,

<PRE>
set Y 0
</PRE>

is executed and its value replaces everything in the square brackets.  After
that substitution, <TT>Y</TT> contains 0 and the overall command line looks like:

<PRE>
set X 0
</PRE>

This latter form is immediately executed.  The overall effect is to set
both <TT>X</TT> and <TT>Y</TT> to 0.

<P><A NAME="2.7a">
<STRONG>Exercise 2.7a</STRONG> </A><DL><DD>
  Explain the following.

<PRE>
%  [puts {Hellow Orld}]
Hellow Orld
empty command name ""
</PRE>
<P>
<A HREF="2.11.html#Sol2.7a" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.11.html#Sol2.7a">Solution</A></DL>


<P> Now, we are ready to finish the <TT>while</TT> command in the file copying
example.  As a reminder, it looks like this.

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

<P> Try to figure out what the pseudostep should be before reading the
answer which comes next.

<P> The pseudostep will use command substitution to execute a procedure which
reads an input line.  The result of command substitution will be
compared with -1 to see if the attempt to read an input line succeeded.

<P>  So the <TT>while</TT> statement looks like this:

<PRE>
while {-1 != [gets $InFile Line]} {
  puts $OutFile $Line
}
</PRE>

<P> <P><A NAME="2.7b">
<STRONG>Exercise 2.7b</STRONG> </A><DL><DD>
   

<OL>
<P><LI> Assume that <TT>Z</TT> contains 2.  Describe the substitutions that take place
when this command is executed.

<PRE>
set X [set Y $Z]
</PRE>

Pay particular attention to when the substitutions happen.

<P> <P><LI> Use command substitution to finish the
statement,

<PRE>
set Z ...
</PRE>

so that <TT>Z</TT> will contain a string that consists of whatever is in the
variable <TT>X</TT> followed immediately by "_ext" (without the quotes).
</OL>
<P>
<A HREF="2.11.html#Sol2.7b" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.11.html#Sol2.7b">Solution</A></DL>


<!-- 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.6.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.6.html">section</A><WBR>
<STRONG>Next</STRONG>
<A HREF="2.8.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.8.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 + -