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

📄 0509-0511.html

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTML
字号:


<HTML>

<HEAD>

<TITLE>Developer.com - Online Reference Library - 0672311739:RED HAT LINUX 2ND EDITION:tcl and tk Programming</TITLE>

<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>

 -->




<!-- ISBN=0672311739 //-->

<!-- TITLE=RED HAT LINUX 2ND EDITION //-->

<!-- AUTHOR=DAVID PITTS ET AL //-->

<!-- PUBLISHER=MACMILLAN //-->

<!-- IMPRINT=SAMS PUBLISHING //-->

<!-- PUBLICATION DATE=1998 //-->

<!-- CHAPTER=25 //-->

<!-- PAGES=0499-0528 //-->

<!-- UNASSIGNED1 //-->

<!-- UNASSIGNED2 //-->









<P><CENTER>

<a href="0506-0508.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0512-0514.html">Next</A>

</CENTER></P>



<A NAME="PAGENUM-509"><P>Page 509</P></A>





<TABLE WIDTH="360">

<TR><TD>

tan(x)

</TD><TD>

Tangent of x (x in radians)

</TD></TR>





<TR><TD>

tanh(x)

</TD><TD>

Hyperbolic tangent of x

</TD></TR>



<TR><TD>

atan(x)

</TD><TD>

Arctangent of x (-pi/2 to pi/2)

</TD></TR>



<TR><TD>

exp(x)

</TD><TD>

e raised to the power of x

</TD></TR>



<TR><TD>

log(x)

</TD><TD>

Natural log of x

</TD></TR>





<TR><TD>

log10(x)

</TD><TD>

Log base 10 of x

</TD></TR>



<TR><TD>

sqrt(x)

</TD><TD>

The square root of x

</TD></TR>

</TABLE>









<P>The following math function takes two number arguments:



<TABLE WIDTH="360">

<TR><TD>

pow(x,y)

</TD><TD>

x raised to the power of y

</TD></TR>

</TABLE>











<P>This is used as follows:

</P>



<!-- CODE SNIP //-->

<PRE>

set a 2;

set b [expr pow($a,3)];

puts $b;

</PRE>

<!-- END CODE SNIP //-->









<P>The output will be 8.0, the value of 2 raised to the third

power.

</P>









<H4><A NAME="ch25_ 13">





Quoting and Substitution

</A></H4>









<P>Quoting and substitution are both used heavily in relation to variables. You saw the most

basic version of quoting (using double quotes to make strings) and substitution earlier in this

chapter. tcl supports one more type of quoting, brace quoting, and one more type of

substitution, command substitution.

</P>









<P>To review, the most common use of double quotes is to create strings with embedded whitespace:

</P>



<!-- CODE SNIP //-->

<PRE>

set kiwi &quot;Fresh from New Zealand&quot;;

</PRE>

<!-- END CODE SNIP //-->









<P>Double quotes can also be used to make multiline strings:

</P>



<!-- CODE SNIP //-->

<PRE>

set kiwi &quot;Fresh from

New Zealand 3 for a dollar&quot;;

</PRE>

<!-- END CODE SNIP //-->









<P>In addition to making multiline strings, the standard ANSI C language escape sequences

can be used in tcl strings:

</P>



<!-- CODE SNIP //-->

<PRE>

set kiwi &quot;Fresh from New Zealand\n\t3 for a dollar.&quot;

</PRE>

<!-- END CODE SNIP //-->









<P>This outputs the following:

</P>



<!-- CODE SNIP //-->

<PRE>

Fresh from New Zealand

3 for a dollar.

</PRE>

<!-- END CODE SNIP //-->









<P>In addition to this, the two types of substitution can be applied within double-quoted

strings. The first type of substitution, variable substitution, is explained in the &quot;Variables&quot; section,

earlier in this chapter. In a double-quoted string, you can access the value of a variable by

preceding the variable's name with $. Thus the following commands

</P>



<A NAME="PAGENUM-510"><P>Page 510</P></A>





<!-- CODE SNIP //-->

<PRE>

set fruit kiwi;

set place &quot;New Zealand&quot;;

set how_many 3;

puts &quot;$fruit, fresh from $place, $how_many for a dollar&quot;;

</PRE>

<!-- END CODE SNIP //-->









<P>output this:

</P>



<!-- CODE SNIP //-->

<PRE>

kiwi, fresh from New Zealand, 3 for a dollar

</PRE>

<!-- END CODE SNIP //-->









<P>The other type of substitution is command substitution. A command substitution block

begins with a left bracket ([) and ends with a right bracket

(]). For example,

</P>



<!-- CODE SNIP //-->

<PRE>

set len_in 2;     puts &quot;$len_in inches is [expr $len_in*2.54] cm&quot;;

</PRE>

<!-- END CODE SNIP //-->









<P>outputs

</P>



<!-- CODE SNIP //-->

<PRE>

2 inches is 5.08 cm

</PRE>

<!-- END CODE SNIP //-->









<P>The 5.08 is the result of the command

</P>



<!-- CODE SNIP //-->

<PRE>

expr $len_in*2.54

</PRE>

<!-- END CODE SNIP //-->









<P>Because this command is in brackets, the value that it returns is substituted in. In this case,

the tcl command expr is used, but any tcl command can be placed between brackets.

Command substitution can be used in most commands, and is not limited to double-quoted

commands. For example, the commands

</P>



<!-- CODE SNIP //-->

<PRE>

set len_in 2;

set len_cm [expr $len_in*2.54];

puts &quot;$len_in inches is $len_cm cm&quot;;

</PRE>

<!-- END CODE SNIP //-->









<P>produce the same output as

</P>



<!-- CODE SNIP //-->

<PRE>

set len_in 2;     puts &quot;$len_in inches is [expr $len_in*2.54] cm&quot;;

</PRE>

<!-- END CODE SNIP //-->









<P>The other type of quoting available in tcl is brace quoting, which is similar to the

quoting using single quotes in UNIX shells. Brace quoting creates a string with the given characters,

no substitution (command or variable) takes place, and the C language

escape sequences are not interpreted. For example, the command

</P>



<!-- CODE SNIP //-->

<PRE>

puts &quot;This\nis a\nmulti-line\nstring&quot;

</PRE>

<!-- END CODE SNIP //-->









<P>produces the following output:

</P>



<!-- CODE SNIP //-->

<PRE>

This

is a

multi-line

string

</PRE>

<!-- END CODE SNIP //-->









<P>The command

</P>



<!-- CODE SNIP //-->

<PRE>

puts {This\nis a\nmulti-line\nstring}

</PRE>

<!-- END CODE SNIP //-->









<P>produces the following output:

</P>



<!-- CODE SNIP //-->

<PRE>

This\nis a\nmulti-line\nstring

</PRE>

<!-- END CODE SNIP //-->



<A NAME="PAGENUM-511"><P>Page 511</P></A>













<P>To get tabs, newlines, and other special characters in a brace-quoted string, they must be

entered physically:

</P>



<!-- CODE SNIP //-->

<PRE>puts {This

is a

multi-line

string}

</PRE>

<!-- END CODE SNIP //-->









<P>This will produce the desired output. The real use for brace-quoted strings comes when

certain characters with special meanings need to be given as values for variables. For example, the <BR>

commands

</P>



<!-- CODE SNIP //-->

<PRE>set price 1.00;

puts &quot;Pears, $$price per pound&quot;;

</PRE>

<!-- END CODE SNIP //-->









<P>output

</P>



<!-- CODE SNIP //-->

<PRE>Pears, $1.00 per pound

</PRE>

<!-- END CODE SNIP //-->









<P>but the $$price has the potential to be confusing, so it would be better if the variable

price had the value $1.00. You could use brace quoting to achieve the following:

</P>



<!-- CODE SNIP //-->

<PRE>set price {$1.00};

puts &quot;Pears, $price per pound&quot;;

</PRE>

<!-- END CODE SNIP //-->









<P>The other use of brace quoting is to defer evaluation and is used in control structures and

procedure definitions. In such cases, the values of variables are substituted in after the entire

block is read.

</P>









<H4><A NAME="ch25_ 14">





Flow Control&#151;if and switch

</A></H4>









<P>tcl provides several commands for flow control and supports all the standard ANSI C

comparison operators for both string and numeric data.

</P>









<P>This section starts with the if/elseif/else commands. The simplest

if statement is one like the following:

</P>



<!-- CODE SNIP //-->

<PRE>if {$x &lt; 0} {

set x 10;

}

</PRE>

<!-- END CODE SNIP //-->









<P>This example only has one line in the body of the

if clause, but any number of lines and subblocks can be added. If additional tests need to be performed, each test is given in

parentheses as follows:

</P>



<!-- CODE SNIP //-->

<PRE>if { ($x == &quot;SJ&quot;) || ($x == &quot;LA&quot;) } {

puts &quot;You Live in California!&quot;;

}

</PRE>

<!-- END CODE SNIP //-->









<P>Tests can be nested as in the following example:

</P>



<!-- CODE SNIP //-->

<PRE>if { ( ($arch == &quot;ppc&quot;) || ($arch == &quot;intel&quot;) ) &amp;&amp; ($os != &quot;Linux&quot;) } {

puts &quot;Get Linux!&quot;;

}

</PRE>

<!-- END CODE SNIP //-->



<P><CENTER>

<a href="0506-0508.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0512-0514.html">Next</A>

</CENTER></P>









</td>
</tr>
</table>

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -