expr one-or-more-arguments |
|
The other outcome of an expression is string
White space may be used between operands, operators and parentheses; it is ignored by the expression processor.
|
|
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
|
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
|
| 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) |
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 |
NOTE:
expr {"abc" < "def"} // 1 (true)
// expr receives: "abc" < "def"
expr {"abc" > "def"} // 0 (false)
// expr receives: "abc" < "def"
string compare "12" "3" // -1
|