-->
Page 506
This occurs because when $ precedes a variable's name, the value of the variable is substituted in before the command is executed.
The simplest form of string manipulation is the append command, which concatenates multiple strings and variables together. As an illustration, the following commands
set str1 "Begin"; append str1 " a String"; set str2 " even more text"; append str1 " with some text" " and add" $str2 " to it."; puts $str1;
output
Begin a String with some text and add even more text to it.
You can achieve the same results using the following commands:
set str1 "Begin"; set str1 "$str1 a String"; set str2 " even more text"; set str1 "$str1 with some text and add$str2 to it.";
But this will be slower than using append because append does not do character copying as set does.
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
string option string1 string2
where string1 and string2 can either be a literal strings ("this is a string") or variables, and option is one of the following:
compare | 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) |
first | Returns the index of the first occurrence of string1 in string2, or -1 if string1 does not occur in string2 |
last | Returns the index of the last occurrence of string1 in string2, or -1 if string1 does not occur in string2 |
The following options to the string command interpret string2 as a list of characters to trim from string1:
trim | Removes any leading and trailing characters present in string2 from string1 |
trimleft | Removes any leading characters present in string2 from string1 |
Page 507
trimright | Removes any trailing characters present in string2 from string1 |
The following options to the string command only take string1 as an argument:
length | Returns the number of characters in string1 |
tolower | Returns a new string with all of the characters in string1 converted to lowercase |
toupper | Returns a new string with all of the characters in string1 converted to uppercase |
Now let's look at a few examples. First, make a string and get its length:
set str " Here Is A Test String "; string length $str;
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:
string first "st" $str; string last "st" $str
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:
string last "st" [string tolower $str];
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:
string length [string trim $str " "];
The value 21 is returned, which means that the first and last spaces were stripped off.
tcl provides two commands for manipulating numeric variables and constants: incr and expr.
The incr command gives tcl an equivalent to the C language operators +=, -=, ++, and --. The basic syntax is
incr variable integer
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
usage. First, create a variable and do an incr on it:
set a 81; incr a; puts $a;
Page 508
$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:
incr a -3 puts $a
Note that $a has a value of 79. One last point is that the integer can be the value of a variable:
set a 6; set b 9; incr a $b; puts $a;
$a has a value of 15.
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.
When any mathematical operations are required, they must be preceded by the expr command. For example, the commands
set a 20; set b 4; set c $a/$b; puts $c;
result in the output
20/4
rather than 5, the desired result. To get the right answer, use the expr command:
set c [expr $a / $b];
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
expr function number
Some of the functions that expr understands, along with the values they return, are the
following:
abs(x) | Absolute value of x |
round(x) | The integer value resulting from rounding x |
cos(x) | Cosine of x (x in radians) |
cosh(x) | Hyperbolic cosine of x |
acos(x) | Arccosine of x (0 to pi) |
sin(x) | Sine of x (x in radians) |
sinh(x) | Hyperbolic sine of x |
asin(x) | Arcsine of x (-pi/2 to pi/2) |