expr is for Tcl-to-do math type operations. It takes all of its arguments ("2 + 2" for example) and evaluates the result as a Tcl "expression", and returns the value. Many commands expr
use behind the scenes of order if
to evaluate test expressions, such as, and loops. for
tip: enclosing the arguments to in expr
curly braces would result in faster code. So do expr {$i * 10}
instead of simplyexpr $i * 10
1) Operators
- + ~ !
unary minus, unary Plus, bit-wise not, logical not. None of these operators is applied to string operands, and bit-wise isn't may is applied only to integers.
**
-
-
Exponentiation (works on both floating-point numbers and integers)
-
-
-
-
-
-
-
-
- * / %
-
- Multiply, divide, remainder. None of these operators is applied to string operands, and remainder is applied only to integers. The remainder always has the same sign as the divisor and an absolute value smaller than the divisor.
-
-
-
-
-
-
-
-
- + -
-
- Add and subtract. Valid for any numeric operands.
-
-
-
-
-
-
-
-
-
- << >>
-
- Left and right (bit) shift. Valid for integer operands only.
-
-
-
-
-
-
-
-
-
-
- < > <= >=
-
- Relational operators:less, greater, less than or equal, and greater than or equal. Each operator produces 1 if the condition is true, 0 otherwise. These operators may being applied to numeric operands as well as strings, in which case string comparison is used.
-
-
-
-
-
-
-
-
-
-
- eq ne in NI
-
- Compare-strings for equality (
eq
) or inequality (
ne
). And both operators for checking if a string is Containe D in a list (
in
) or not (
ni
). These operators all return 1 (true) or 0 (false). Using These operators ensures that the operands is regarded exclusively as strings (and lists), not as possible numbers.
-
-
-
-
-
-
- &
-
- Bit-wise and. Valid for integer operands only.
^
Bit-wise exclusive OR. Valid for integer operands only.
|
Bit-wise OR. Valid for integer operands only.
&&
Logical and. Produces a 1 result if both operands is Non-zero, 0 otherwise. Valid for numeric operands only (integers or floating-point).
||
Logical OR. Produces a 0 result if both operands is zero, 1 otherwise. Valid for numeric operands only (integers or floating-point).
X?y:z
If-then-else. If x evaluates to Non-zero, then the result is the value of Y. Otherwise The result is the value of Z. The x operand must has a numeric value.
2) Example
1Set X 1002Set Y 2563Set Z [Expr {$Y +$X}]4Set Z_label"$Y plus $X is"5 6Puts"$Z _label $Z"7Puts"The square root of $Y is [expr {sqrt ($Y)}]\n"8 9Puts"Because of the precedence rules \ "5 +-3 * 4\" is: [Expr {-3 * 4 + 5}]"TenPuts"Because of the parentheses \ "(5 +-3) * 4\" is: [Expr {(5 +-3) * 4}]" One ASet A 3 -Set B 4 -Puts"The hypotenuse of a triangle: [Expr {hypot ($A, $B)}]" the - # - #The trigonometric functions work with radians ... - # +Set pi6 [Expr {3.1415926/6.0}] -Puts"The sine and cosine of PI/6: [Expr {sin ($pi 6)}] [expr {cos ($pi 6)}]" + A # at #Working with Arrays - # -Set a (1) 10 -Set A (2) 7 -Set A (3) 17 -Set B 2 inPuts"Sum: [Expr {$a (1) + $a ($b)}]"
Tcl's Math