Operands

A Tcl expression consists of a combination of operands, operators, and parentheses. White space can be used between the operands and operators and parentheses; it is ignored by the expression processor. Where possible, operands are interpreted as integer values.

Integer values can be specified in:

  • Decimal. This is the normal case.
  • Octal, if the first character of the operand is 0.
  • Hexadecimal, if the first two characters of the operand are 0x.

If an operand does not have one of the integer formats given above, then it is treated as a floating-point number, if possible.

Floating-point numbers can be specified in any of the ways that are accepted by an ANSI-compliant C compiler. The f, F, l, and L suffixes are not permitted in most installations.

For example, all of these are valid floating-point numbers: 2.1, 3., 6e4, 7.91e+16.

If no numeric interpretation is possible, then an operand is left as a string. Only a limited set of operators can be applied to it.

Operands can be specified as:

  • A numeric value: integer or floating-point.
  • A Tcl variable, using standard $ notation. The variable's value is used as the operand.
  • A string that is enclosed in double quotes. The expression parser performs backslash, variable, and command substitutions on the information between the quotes, and use the resulting value as the operand.
  • A string that is enclosed in braces. The characters between the open brace and matching close brace are used as the operand without any substitutions.
  • A Tcl command that is enclosed in brackets. The command is run and its result is used as the operand.
  • A mathematical function whose arguments have any of the above forms for operands, such as sin($x).

Where substitutions happen, for example, inside quoted strings, they are performed by the expression processor. An additional layer of substitution might already have been performed by the command parser before the expression processor was called. It is usually best to enclose expressions in braces to prevent the command parser from performing substitutions on the contents.

For some examples of expressions, variable a has the value 3 and variable b has the value 6.

The commands on the left side of each of the lines produce the value on the next line:

mpexpr 3.1 + $a 
6.1 
mpexpr 2 + "$a.$b" 
5.6 
mpexpr 4*[llength "6 2"] 
8 
mpexpr {{word one} < "word $a"} 
0