Overview
| Artifact ID: | 03aae72013633d20a1e5bdcb0c5968db99ef41d3b8ce80791bf101ca688f9846 |
|---|---|
| Ticket: | aa69bce71e214872af462de3a3050a46eec61385
expr comparison operator result does not follow the documentation |
| User & Date: | sebres on 2024-03-11 23:06:37 |
Changes
- closedate changed to: "2460381.46293034"
- closer changed to: "sebres"
- icomment:
From point of view of the Tcl-parser the tokens `"12"` and `12` (but also `{12}`) are the same. See [Tcl language syntax](https://tcl.tk/man/tcl/TclCmd/Tcl.htm) for basic details how Tcl-parser works. The mentioned documentation clarifying only that there is a fallback operation which would compare them as string, **if one of the operands is not numeric**. However by the operators like `<`, it'd always prefer numeric comparison (no matter whether it is enclosed in quotes or not). Since 8.7 ([TIP 461](https://core.tcl-lang.org/tips/doc/trunk/tip/461.md)), there is a lot of other operators which always use string comparison. In this case: ``` % expr {12 lt 2} 1 % expr {"12" lt "2"} 1 % expr {{12} lt {2}} 1 ``` (again, no matter they are enclosed in quotes or not). Before that (for 8.5 or 8.6) there are only two string comparison operators available - `eq` and `ne`, so one'd use `[string compare]` either. Another possibility would be to use the trick with string-concatenation to word-tokens, in order to force string comparison: ``` % set a 12; set b 2; expr {$a < $b} 0 % set a 12; set b 2; expr {"s$a" < "s$b"} 1 ``` - login: "sebres"
- mimetype: "text/x-markdown"
- resolution changed to: "Invalid"
- status changed to: "Closed"