Artifacts Associated With Ticket aa69bce71e214872
Ticket change [5461560ec6] (rid 222896) by anonymous on 2024-03-11 21:26:09:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/x-fossil-plain"
- comment initialized to:
I wanted to know if the 'expr' command when using the < operator compares its arguments as numbers or as strings. So I checked the documentation for expr. This is what it says: >> These operators may be applied to strings as well as numeric operands, in which case string comparison is used. << https://tcl.tk/man/tcl/TclCmd/expr.htm#M12 From this, I expect that I can compare two string, or I can compare two numbers, both will be ok for the operator. It should always compare as strings - as it says that strig comparison is used. But the implementation does not follow it % expr {12<2} 0 % expr {"12"<"2"} 0 When compared as strings the first should be smaller than the second. The result should be 1. The string comparison should be like this: % string compare 12 2 -1 I.e. the first string is less than the second. Or maybe the documentation is wrong? - foundin initialized to: "8.6"
- is_private initialized to: "0"
- login: "anonymous"
- priority initialized to: "5 Medium"
- private_contact initialized to: "eb1592a11067c658ae62686070571429a9282829"
- resolution initialized to: "None"
- severity initialized to: "Minor"
- status initialized to: "Open"
- submitter initialized to: "anonymous"
- subsystem initialized to: "16. Commands A-H"
- title initialized to:
expr comparison operator result does not follow the documentation
- type initialized to: "Bug"
Ticket change [03aae72013] (rid 222909) by sebres on 2024-03-11 23:06:37:
- 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"
Ticket change [70c4fe56c0] (rid 222940) by oehhar on 2024-03-12 08:14:04:
- closedate changed to: "2460381.84310304"
- closer changed to: "oehhar"
- icomment:
please use "string compare" to compare strings in a relyable way with TCL 8.6.
- login: "oehhar"
- mimetype: "text/x-fossil-wiki"
- username: "oehhar"