📄 0506-0508.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="0503-0505.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0509-0511.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-506"><P>Page 506</P></A>
<P>This occurs because when $ precedes a variable's name, the value of the variable is
substituted in before the command is executed.
</P>
<H4><A NAME="ch25_ 11">
Manipulating String Values
</A></H4>
<P>The simplest form of string manipulation is the
append command, which concatenates multiple strings and variables together. As an illustration, the following commands
</P>
<!-- CODE //-->
<PRE>set str1 "Begin";
append str1 " a String";
set str2 " even more text";
append str1 " with some text" " and add" $str2 " to it.";
puts $str1;
</PRE>
<!-- END CODE //-->
<P>output
</P>
<!-- CODE SNIP //-->
<PRE>
Begin a String with some text and add even more text to it.
</PRE>
<!-- END CODE SNIP //-->
<P>You can achieve the same results using the following commands:
</P>
<!-- CODE SNIP //-->
<PRE>set str1 "Begin";
set str1 "$str1 a String";
set str2 " even more text";
set str1 "$str1 with some text and add$str2 to it.";
</PRE>
<!-- END CODE SNIP //-->
<P>But this will be slower than using append because
append does not do character copying as set does.
</P>
<P>For more advanced string manipulation, tcl provides the
string command, which understands a whole host of options. The basic syntax
of the string command is
</P>
<!-- CODE SNIP //-->
<PRE>
string option string1 string2
</PRE>
<!-- END CODE SNIP //-->
<P>where string1 and string2 can either be a literal strings
("this is a string") or variables, and
option is one of the following:
</P>
<TABLE WIDTH="360">
<TR><TD>
compare
</TD><TD>
Returns -1, 0, or 1 depending on whether or not
string1
is lexographically less than, equal to, or greater
than
string2 (similar to the C library function
strcmp)
</TD></TR>
<TR><TD>
first
</TD><TD>
Returns the index of the first occurrence of
string1 in
string2, or -1 if string1 does not
occur in string2
</TD></TR>
<TR><TD>
last
</TD><TD>
Returns the index of the last occurrence of
string1 in
string2, or -1 if string1 does not occur in
string2
</TD></TR>
</TABLE>
<P>The following options to the string command interpret
string2 as a list of characters to trim from
string1:
</P>
<TABLE WIDTH="360">
<TR><TD>
trim
</TD><TD>
Removes any leading and trailing characters present
in string2 from string1
</TD></TR>
<TR><TD>
trimleft
</TD><TD>
Removes any leading characters present in
string2 from string1
</TD></TR>
</TABLE>
<A NAME="PAGENUM-507"><P>Page 507</P></A>
<TABLE WIDTH="360">
<TR><TD>
trimright
</TD><TD>
Removes any trailing characters present in
string2 from string1
</TD></TR>
</TABLE>
<P>The following options to the string command only take
string1 as an argument:
</P>
<TABLE WIDTH="360">
<TR><TD>
length
</TD><TD>
Returns the number of characters in string1
</TD></TR>
<TR><TD>
tolower
</TD><TD>
Returns a new string with all of the characters in
string1 converted to lowercase
</TD></TR>
<TR><TD>
toupper
</TD><TD>
Returns a new string with all of the characters in
string1 converted to uppercase
</TD></TR>
</TABLE>
<P>Now let's look at a few examples. First, make a string and get its length:
</P>
<!-- CODE SNIP //-->
<PRE>
set str " Here Is A Test String ";
string length $str;
</PRE>
<!-- END CODE SNIP //-->
<P>This gives a length of 23 (the length option counts whitespace characters). Now get the
location of the first and last occurrences of the string
"st" in $str:
</P>
<!-- CODE SNIP //-->
<PRE>
string first "st" $str;
string last "st" $str
</PRE>
<!-- END CODE SNIP //-->
<P>This gives a value of 13 for the first occurrence of
"st" (corresponding to the occurrence in
Test) and a value of 13 for the last occurrence of
"st" (Test again). What about the
"st" in String? Well, most of the string comparison functions are case- and whitespace-sensitive,
so temporarily convert $str to lowercase and try again:
</P>
<!-- CODE SNIP //-->
<PRE>
string last "st" [string tolower $str];
</PRE>
<!-- END CODE SNIP //-->
<P>This gives a value of 16, which corresponds to the
"st" in String. Finally, strip off the
leading and trailing spaces and get a length for the string:
</P>
<!-- CODE SNIP //-->
<PRE>
string length [string trim $str " "];
</PRE>
<!-- END CODE SNIP //-->
<P>The value 21 is returned, which means that the first and last spaces were stripped
off.
</P>
<H4><A NAME="ch25_ 12">
Manipulating Numeric Values
</A></H4>
<P>tcl provides two commands for manipulating numeric variables and constants:
incr and expr.
</P>
<P>The incr command gives tcl an equivalent to the C language operators
+=, -=, ++, and --. The basic syntax is
</P>
<!-- CODE SNIP //-->
<PRE>
incr variable integer
</PRE>
<!-- END CODE SNIP //-->
<P>where variable must be an integer. The incr command adds the given
integer to the variable; thus decrementing is handled by giving negative integers. Let's demonstrate its <BR>
usage. First, create a variable and do an incr on it:
</P>
<!-- CODE SNIP //-->
<PRE>
set a 81;
incr a;
puts $a;
</PRE>
<!-- END CODE SNIP //-->
<A NAME="PAGENUM-508"><P>Page 508</P></A>
<P>$a has a value of 82. By default, incr is the same as
++; if it is not given an integer argument, it will add one to the named variable. Now decrement
$a by 3:
</P>
<!-- CODE SNIP //-->
<PRE>
incr a -3
puts $a
</PRE>
<!-- END CODE SNIP //-->
<P>Note that $a has a value of 79. One last point is that the integer can be the value of a variable:
</P>
<!-- CODE SNIP //-->
<PRE>
set a 6;
set b 9;
incr a $b;
puts $a;
</PRE>
<!-- END CODE SNIP //-->
<P>$a has a value of 15.
</P>
<P>For more complex mathematical operations, tcl provides the
expr command, which works with all standard ANSI C operators. Operator precedence is mostly the same as in ANSI C.
</P>
<P>When any mathematical operations are required, they must be preceded by
the expr command. For example, the commands
</P>
<!-- CODE SNIP //-->
<PRE>
set a 20;
set b 4;
set c $a/$b;
puts $c;
</PRE>
<!-- END CODE SNIP //-->
<P>result in the output
</P>
<!-- CODE SNIP //-->
<PRE>
20/4
</PRE>
<!-- END CODE SNIP //-->
<P>rather than 5, the desired result. To get the right answer, use the
expr command:
</P>
<!-- CODE SNIP //-->
<PRE>
set c [expr $a / $b];
</PRE>
<!-- END CODE SNIP //-->
<P>In addition to the standard operators of +, -,
*, and /, the expr command can be given several options that enable it to perform mathematical operations. The basic syntax is
</P>
<!-- CODE SNIP //-->
<PRE>
expr function number
</PRE>
<!-- END CODE SNIP //-->
<P>Some of the functions that expr understands, along with the values they return, are the <BR>
following:
</P>
<TABLE WIDTH="360">
<TR><TD>
abs(x)
</TD><TD>
Absolute value of x
</TD></TR>
<TR><TD>
round(x)
</TD><TD>
The integer value resulting from rounding x
</TD></TR>
<TR><TD>
cos(x)
</TD><TD>
Cosine of x (x in radians)
</TD></TR>
<TR><TD>
cosh(x)
</TD><TD>
Hyperbolic cosine of x
</TD></TR>
<TR><TD>
acos(x)
</TD><TD>
Arccosine of x (0 to pi)
</TD></TR>
<TR><TD>
sin(x)
</TD><TD>
Sine of x (x in radians)
</TD></TR>
<TR><TD>
sinh(x)
</TD><TD>
Hyperbolic sine of x
</TD></TR>
<TR><TD>
asin(x)
</TD><TD>
Arcsine of x (-pi/2 to pi/2)
</TD></TR>
</TABLE>
<P><CENTER>
<a href="0503-0505.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0509-0511.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -