**Results of a command - Math 101** !!!!!! '''Previous lesson''' | '''Index''' | '''Next lesson''' !!!!!! The Tcl command for doing math type operations is `expr`. The following discussion of the `expr` command is extracted and adapted from the `expr` man page. Many commands use `expr` behind the scenes in order to evaluate test expressions, such as `if`, `while` and `for` loops, discussed in later sections. All of the advice given here for `expr` also holds for these other commands. `expr` takes all of its arguments ("2 + 2" for example) and evaluates the result as a Tcl "expression" (rather than a normal command), and returns the value. The operators permitted in Tcl expressions include all the standard math functions, logical operators, bitwise operators, as well as math functions like `rand()`, `sqrt()`, `cosh()` and so on. Expressions almost always yield numeric results (integer or floating-point values). '''Performance tip:''' enclosing the arguments to `expr` in curly braces will result in faster code. So do `expr {$i * 10}` instead of simply `expr $i * 10` '''WARNING:''' You should '''always''' use braces when evaluating expressions that may contain user input, to avoid possible security breaches. The `expr` command performs its own round of substitutions on variables and commands, so you should use braces to prevent the Tcl interpreter doing this as well (leading to double substitution). To illustrate the danger, consider this interactive session: ======tcl % set userinput {[puts DANGER!]} [puts DANGER!] % expr $userinput == 1 DANGER! 0 % expr {$userinput == 1} 0 ====== ***OPERANDS*** In the first example, the code contained in the user-supplied inputis evaluated, whereas in the second the braces prevent this potential danger. As a general rule, always surround expressions with braces, whether using `expr` directly or some other command that takes an expression (such as `if` or `while`).
A Tcl expression consists of a combination of operands, operators, and parentheses. White space may be used between operands, operators and parentheses; it is ignored by the expression processor. Where possible, operands are interpreted as integer values. Integer values may be specified in decimal (the normal case), in octal (if the first character of the operand is 0), or in hexadecimal (if the first two characters of the operand are 0x). Note that the octal and hexadecimal conversion takes place differently in the `expr` command than in the Tcl substitution phase. In the substitution phase, a `\x32` would be converted to an ascii "2", while `expr` would convert `0x32` to a decimal 50. If an operand does not have one of the integer formats given above, then it is treated as a floating-point number, if that is possible. Floating-point numbers may be specified in any of the ways accepted by an ANSI-compliant C compiler. For example, all of the following are valid floating-point numbers: ====== 2.1 3. 6E4 7.91e+16 .000001 ====== If no numeric interpretation is possible, then an operand is left as a string (and only a limited set of operators may be applied to it). ====== ====== Note however, that it does not support numbers of the following forms: 2,1 - a decimal comma, instead of a decimal point 2,100 - a thousands separator It is possible to deal with numbers in that form, but you will have to convert these "strings" to numbers in the standard form first. Beware of leading zeros: 0700 is not interpreted as the decimal number 700 (seven hundred), but as the octal number 700 = 7*8*8 = 448 (decimal). ====== ====== Worse, if the number contains a digit 8 or 9 an error results: % expr {0900+1} expected integer but got "0900" (looks like invalid octal number) Octal numbers are in fact a relic of the past, when such number formats were much more common. Operands may be specified in any of the following ways: * As a numeric value, either integer or floating-point. * As a Tcl variable, using standard $ notation. The variable's value will be used as the operand. ***OPERATORS*** The valid operators are listed below, grouped in decreasing order of precedence:eq
) or
inequality (ne
).
and two operators for checking if a string is contained in a list
(in
) or not (ni
).
These operators all return 1 (true) or 0 (false).
Using these operators ensures that the operands are regarded
exclusively as strings (and lists), not as possible numbers:
% expr { "9" == "9.0"} 1 % expr { "9" eq "9.0"} 0
% set x 1 % expr { $x>0? ($x+1) : ($x-1) } 2