TCL/OTCL

# Math 101 in TCL

• Doing Math in TCL

• The expr command is used to evaluate numerical expressions:

 ``` expr one-or-more-arguments ```

• NOTE: The expr command can accept a variable number of input arguments !!!

• The expr command is executed as follows:

 Concatenates args (adding a separator space between two consecute arguments) into a single item NOTE: while the concatenate operation is performed, Tcl will perform substitution on each word ! Evaluate the (one single) result as a Tcl "expression" NOTE: the evaluation rules for Tcl "expression" will be explained later Finally, it returns the value of the evaluation.

• The rules to evaluate a Tcl "expression" is given next.

• Expressions almost always yield numeric results (integer or floating-point values) - even the logical expressions (0 = false, not 0 = true).

The other outcome of an expression is string

• Tcl Expression and their evaluation

• 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.

• How Tcl evaluate expressions:

 Determine the type of each operand Decide on the operation that will be performed on the operand(s).

• How Tcl determines the type of an operand

• Tcl determine the type of an operand by the format

• Procedure:

1. Where possible, an operand is interpreted as integer value.

Integer values may be specified in decimal (base 10), in octal (if the first character of the operand is 0), or in hexadecimal (if the first two characters of the operand are 0x).

Examples:

 ``` 123 (decimal) 0123 (octal) 0x123 (hexadecial) ```

2. If an operand does not have one of the integer formats given above, then Tcl tries to interpret it as a floating-point number

Floating-point numbers may be specified in any of the ways accepted by an ANSI-compliant C compiler (except that the f, F, l, and L suffixes will not be permitted in most installations).

Examples of valid floating-point numbers:

 ``` 2.1 3. 6e4 7.91e+16 ```

3. If the word cannot be interpreted as integer or floating point number, then the operand is assigned the type string

Important: All literal operands in a Tcl expression that are strings must be quoted with either braces {..} or with double quotes "..."

If any non-numeric operand in a Tcl expression is unquoted, the expr will return an error.

Examples:

 ``` expr 12 // Integer // expr receives: 12 // it is a numeric string expr 12.3 // Floating point // expr receives: 12.3 // it is a numeric string expr a // Error: // expr receives: a // it is a non-numeric string - needs quotes !! expr {a} // Error: // expr (still) receives: a // it is a non-numeric string - needs quotes !! expr {"a"} // String: // expr receives: "a" // it is a non-numeric string - it has quotes !! // expr performs a Tcl eveluation on "a"... // Output: a expr {{a}} // String: // expr receives: {a} // it is a non-numeric string - it has quotes !! // expr performs a Tcl eveluation on {a}... // Output: a NOTE: expr [expr {"a"}] // Error: // [expr {"a"}] returns: a // expr a // a is non-numeric - needs quotes !! expr [expr {{"a"}}] // String: // [expr {{"a"}}] returns: {"a"} // expr {"a"} outputs a ```

• More examples (with variables):

 ``` set a 1 // a = 1 set b "abc" // b = abc ("abc" --> abc after substitution) set c {"abc"} // c = "abc" ({"abc"} --> "abc" after substitution) expr \$a // Integer // expr receives: 1 expr \$a + 1 // Integer // expr receives: 1 + 1 expr \$b // Error // expr receives: abc // It's a literal string // ---> need quotes !) // String without quotes... expr \$c // String // expr receives: "abc" // It's a literal string // ---> need quotes and got them !) // Result: abc set x { [puts Hello] } // x = [puts Hello] expr x // Error // Reason: expr receives: x // Literal string without quotes... expr \$x // String // Reason: expr receives: [puts Hello] (1 single arg) // It's NOT a literal string // - don't need quotes !) // eval [puts Hello] --> execute !! --> Hello // Output: Hello expr {\$x} // String // Reason: expr receives: \$x (1 single arg) // It's NOT a literal string // - don't need quotes !) // eval \$x --> substitute --> [puts Hello] // Output: [puts Hello] ```

• Operators in Tcl

• After identifying the type of an operand, one or more operands are then used as operands in operations

• The following table list the more commonly used operators in the order of their precedence:

Operator symbol Operation Applicability for operand types
-   + Unary operators Applicable only to numeric operands (not strings !)
*   /   % Multiply, divide, remainder Applicable only to numeric operands. % only applicable to integers
+   - Binary addition and subtraction Applicable only to numeric operands.
<   >   <=   >= less than, greater than, etc Applicable only to every type of operands.
==   != equal and not equal Applicable only to every type of operands.
eq   ne string equal and string not equal Applicable only to string type of operands.
&& logical AND Applicable only to numeric type of operands. (0 means false, non-zero means true)
|| logical OR Applicable only to numeric type of operands. (0 means false, non-zero means true)

• Simple Tcl expressions:

 ``` expr 17 + 2 // 19 expr "17 + 2" // 19 (NOTE: don't use ".." with expr) // We will see why when we discuss "if" statement expr {17 + 2} // 19 expr 9 / 2 // 4 (quotient) expr 9 % 2 // 1 (remainder) expr 9.0 / 2 // 4.5 (floating point division) expr 9.0 % 2 // Error - Need integers ! expr "6 < 9" // 1 (true) expr 6 >= 9 // 0 (false) ```

 ``` set x 4 // x = 4 expr x + 2 // Error, try to add string and integer // Reason: expr receives: x + 2 // x --> string without quotes expr \$x + 2 // 6 // Reason: expr receives: 4 + 2 // (\$x is replaced by 4) expr "\$x + 2" // 6 // Reason: expr receives: 4 + 2 (1 single arg) expr {\$x + 2} // 6 // Reason: expr receives: \$x + 2 (1 single arg) // Then this Tcl expression is evaluate as follows: // \$x --> substitute --> 4 (int) // 2 --> (literal) integer // + --> add --> 6 ```

NOTE:

• In the command: expr "\$x + 2" , the expr command DID NOT receive \$x + 2 as input - the input it received is: 6

• It is important that you realise this... (otherwise, programming Tcl will be a painful experience...)

• Operators on Strings

• String comparison operators are available:

• ne         test for equalality (!= will also work)
• eq         test for unequalality (== will also work)

• <           test for less than
• >           test for greater than
• <=         test for less than or equal
• >=         test for greater than or equal

• string compare STRING1 STRING2         returns -1 is STRING1 < STRING2 , 0 if equal and 1 if STRING1 > STRING2

• Examples:

 ``` expr {"abc" < "def"} // 1 (true) // expr receives: "abc" < "def" expr {"abc" > "def"} // 0 (false) // expr receives: "abc" < "def" string compare "12" "3" // -1 ```