📄 0503-0505.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="0499-0502.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0506-0508.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-503"><P>Page 503</P></A>
<P>Internally, however, tcl treats all variables as strings. When a variable needs to be
manipulated, tcl allows numbers (real and integer) to be given in all the forms that are understood
by ANSI C. The following are examples of valid numeric values for variables:
</P>
<BR>
74
Integer
0112
Octal, starts with a 0
0x4a
Hexadecimal, starts with 0x
74.
Real
74.0
Real
7.4e1
Real
7.4e+1
Real
<P>Other values are treated as strings and will generate errors if used in mathematical
expressions.
</P>
<H4><A NAME="ch25_ 10">
Variables
</A></H4>
<P>tcl defines two types of variables, scalars and arrays. To create a scalar variable and assign it
a value, use the set command. For example,
</P>
<!-- CODE SNIP //-->
<PRE>
set banana 1;
</PRE>
<!-- END CODE SNIP //-->
<P>creates the variable banana and gives it a value of
1. To set the value of banana to something different, simply use
set again:
</P>
<!-- CODE SNIP //-->
<PRE>
set banana "Fresh from Brazil";
</PRE>
<!-- END CODE SNIP //-->
<P>Now the variable banana has the value "Fresh from
Brazil". The double quotes tell tcl that all the characters including the spaces make up the value of the variable. (Quoting and
substitution are covered later in this chapter in the section "Quoting and Substitution.")
</P>
<P>To print out the value of banana, use the puts command:
</P>
<!-- CODE SNIP //-->
<PRE>
puts $banana;
</PRE>
<!-- END CODE SNIP //-->
<P>This prints the value of the variable banana to the standard output (sometimes referred to
as STDOUT). Putting $ before the name of the variable tells
tcl to access the value assigned to that variable. This convention, known as variable substitution, is similar to conventions used
in UNIX shells.
</P>
<P>To created a one-dimensional array, enter the following:
</P>
<!-- CODE SNIP //-->
<PRE>
set fruit(0) banana;
set fruit(1) orange;
</PRE>
<!-- END CODE SNIP //-->
<P>This creates the array variable fruit and assigns to the two named items,
0 and 1, the values banana and orange. The assignments to array indexes need not be in order. The commands
</P>
<!-- CODE SNIP //-->
<PRE>
set fruit(100) peach;
set fruit(2) kiwi;
set fruit(87) pear;
</PRE>
<!-- END CODE SNIP //-->
<A NAME="PAGENUM-504"><P>Page 504</P></A>
<P>create only three items in the array fruit. This is because arrays in
tcl are like associative arrays, which associate a "key" with a value. Arrays in
tcl associate a given string with another string. This makes it possible to have array indexes that are not numbers:
</P>
<!-- CODE SNIP //-->
<PRE>
set fruit(banana) 100
</PRE>
<!-- END CODE SNIP //-->
<P>This sets the value of item banana in the array
fruit to 100. The assigned values need not be numeric:
</P>
<!-- CODE SNIP //-->
<PRE>
set food(koala) eucalyptus;
set food(chipmunk) acorn;
</PRE>
<!-- END CODE SNIP //-->
<P>To access the value stored in a one-dimensional array variable, use the
$ convention:
</P>
<!-- CODE SNIP //-->
<PRE>
puts $food(koala);
</PRE>
<!-- END CODE SNIP //-->
<P>This prints out the value stored in the array
food at index koala. The array index can also be a variable:
</P>
<!-- CODE SNIP //-->
<PRE>
set animal chipmunk;
puts $food($animal);
</PRE>
<!-- END CODE SNIP //-->
<P>These commands will output acorn, given the previous assignments.
</P>
<P>Multidimensional arrays are a simple extension of one-dimensional arrays. They are set as <BR>
follows:
</P>
<!-- CODE SNIP //-->
<PRE>
set myarray(1,1) 0;
</PRE>
<!-- END CODE SNIP //-->
<P>This sets the value of the item at 1,1 in the array
myarray to be 0. By separating the indexes by commas, you can make arrays of three, four, or more dimensions:
</P>
<!-- CODE SNIP //-->
<PRE>
set array(1,1,1,1,1,1) "foo";
</PRE>
<!-- END CODE SNIP //-->
<P>In addition to setting array values, tcl provides the
array command for getting information about arrays and the
parray command for printing out information about arrays. First, take
a look at the parray command. Given the declarations
</P>
<!-- CODE SNIP //-->
<PRE>
set food(koala) eucalyptus;
set food(chipmunk) acorn;
set food(panda) bamboo;
</PRE>
<!-- END CODE SNIP //-->
<P>the command
</P>
<!-- CODE SNIP //-->
<PRE>
parray food
</PRE>
<!-- END CODE SNIP //-->
<P>will produce the following output:
</P>
<!-- CODE SNIP //-->
<PRE>
food(chipmunk) = acorn
food(koala) = eucalyptus
food(panda) = bamboo
</PRE>
<!-- END CODE SNIP //-->
<P>Now look at the array command and its arguments, which are used to get information
about an array and its elements. The basic syntax for an
array command is as follows:
</P>
<!-- CODE SNIP //-->
<PRE>
array option arrayname
</PRE>
<!-- END CODE SNIP //-->
<A NAME="PAGENUM-505"><P>Page 505</P></A>
<P>The supported options are discussed later in this section.
</P>
<P>One of the most frequently used pieces of information about an array is its size. Given
the declarations
</P>
<!-- CODE SNIP //-->
<PRE>
set fruit(0) banana;
set fruit(1) peach;
set fruit(2) pear;
set fruit(3) apple;
</PRE>
<!-- END CODE SNIP //-->
<P>The command
</P>
<!-- CODE SNIP //-->
<PRE>
array size fruit;
</PRE>
<!-- END CODE SNIP //-->
<P>will return 4. This number is often useful in loops.
</P>
<P>Because arrays can have nonsequential or nonnumeric indexes, the
array command provides an option for getting elements from an array. Assuming that the
food array has been defined as presented earlier, the first thing you need to do to start getting elements is to use
startsearch through the array. This is accomplished by first getting a search ID for the array:
</P>
<!-- CODE SNIP //-->
<PRE>
set food_sid [array startsearch food];
</PRE>
<!-- END CODE SNIP //-->
<P>The command
</P>
<!-- CODE SNIP //-->
<PRE>
array startsearch food
</PRE>
<!-- END CODE SNIP //-->
<P>returns a string, which is the name of the search (see the section called "Quoting and
Substitution"). You will need this for future reference, so set its value to that of a variable, in this
case food_sid.
</P>
<P>To get the first element (and every subsequent element) of the
food array, use the following:
</P>
<!-- CODE SNIP //-->
<PRE>
array nextelement food $food_sid;
</PRE>
<!-- END CODE SNIP //-->
<P>When the array search is done, terminate the search for the array using
</P>
<!-- CODE SNIP //-->
<PRE>
array donesearch food $food_sid;
</PRE>
<!-- END CODE SNIP //-->
<P>One other option to the array command that is frequently in use while iterating through
an array is the anymore option. It returns true (a value of
1) if there are any more items in the search. For example,
</P>
<!-- CODE SNIP //-->
<PRE>
array anymore food $food_sid;
</PRE>
<!-- END CODE SNIP //-->
<P>returns 1 the first two times it is used with the
food array declared earlier.
</P>
<P>To dispose of a variable (scalar or array), use the
unset command:
</P>
<!-- CODE SNIP //-->
<PRE>
unset banana;
</PRE>
<!-- END CODE SNIP //-->
<P>This unsets the variable banana. If you used unset
$banana (assuming that banana was set to the value shown earlier) instead of just
banana, you would get an error like:
</P>
<!-- CODE SNIP //-->
<PRE>
can't unset "0": no such variable
</PRE>
<!-- END CODE SNIP //-->
<P><CENTER>
<a href="0499-0502.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0506-0508.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -