Tcl expr operands

Tcl operands are treated as integers, where feasible. They may be specified as decimal, binary (first two characters must be 0b), hexadecimal (first two characters must be 0x), or octal (first two characters must be 0o). Care should be taken when passing integers with a leading 0, for example 08, as the interpreter would evaluate 08 as an illegal octal value. If no integer formats are included, the command will evaluate the operand as a floating-point numeric value. For scientific notations, the character e (or E) is inserted as appropriate. If no numeric interpretation is feasible, the value will be evaluated as a string. In this case, the value must be enclosed within double quotes or braces. Please note that not all operands are accepted by all operators. To avoid inadvertent variable substitution, it is always best to enclose the operands within braces. For example, take a look at the following:

  • expr 1+1*3 will return a value of 4.
  • expr (1+1)*3 will return a value of 6.

Operands may be presented in any of the following:

Operand

Explanation

Numeric

Integer and floating-point values may be passed directly to the command.

Boolean

All standard Boolean values (true, false, yes, no, 0, or 1) are supported.

Tcl variable

All referenced variables (in Tcl, a variable is referenced using the $ notation, for example, myVariable is a named variable, whereas $myVariable is the referenced variable).

Strings

(in double quotes)

Strings contained within double quotes may be passed with no need to include backslash, variable, or command substitution, as these are handled automatically (see the chapter on String Expressions and Handling for clarification on these terms and their usage).

Strings

(in braces)

Strings contained within braces will be used with no substitution.

Tcl commands

Tcl commands must be enclosed within square braces.

The command will be executed and the mathematical function is performed on the return value.

Named functions

Functions, such as sine, cosine, and so on.

Tcl supports a subset of the C programming language math operators and treats them in the same manner and precedence. If a named function (such as sine) is encountered, expr automatically makes a call to the mathfunc namespace to minimize the syntax required to obtain the value.

Tcl expr operators may be specified as noted in the following table, in the descending order of precedence:

Operator

Explanation

- + ~ !

Unary minus, unary plus, bitwise NOT and logical NOT.

Cannot be applied to string operands.

Bit-wise NOT may be applied to only integers.

**

Exponentiation

Numeric operands only.

*/ %

Multiply, divide, and remainder.

Numeric operands only.

+ -

Add and subtract.

Numeric operands only.

<< >>

Left shift and right shift.

Integer operands only.

A right shift always propagates the sign bit.

< > <= >=

Boolean Less, Boolean Greater, Boolean Less Than or Equal To, Boolean Greater Than or Equal To (A value of 1 is returned if the condition is true, otherwise a 0 is returned).

If utilized for strings, string comparison will be applied.

== !=

Boolean Equal and Boolean Not Equal (A value of 1 is returned if the condition is true, otherwise a 0 is returned).

eq ne

Boolean String Equal and Boolean String Not Equal (A value of 1 is returned if the condition is true, otherwise a 0 is returned).

Any operand provided will be interpreted as a string.

in ni

List Containment and Negated List Containment (A value of 1 is returned if the condition is true, otherwise a 0 is returned).

The first operand is treated as a string value, the second as a list.

&

Bitwise AND

Integers only.

^

Bitwise Exclusive OR

Integers only.

|

Bitwise OR

Integers only.

&&

Logical AND (a value of 1 is returned if both operands are 0, otherwise a 1 is returned).

Boolean and numeric (integer and floating-point) operands only.

x?y:z

If-then-else (if x evaluates to non-zero, then the return is the value of y, otherwise the value of z is returned).

The x operand must have a Boolean or a numeric value.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.131.212