Text string bug
Moderator: Klaus
Re: Text string bug
I should point out that the behavior observed here is a semantic of the comparison operators - it has nothing to do with how literals are interpreted.
Just like many other scripting languages (e.g. JavaScript), comparison operators try numeric comparison first and then string.
EDIT: JavaScript is not that good an example (it only does numeric if the types of the two sides are mixed); PHP is a better one!
Just like many other scripting languages (e.g. JavaScript), comparison operators try numeric comparison first and then string.
EDIT: JavaScript is not that good an example (it only does numeric if the types of the two sides are mixed); PHP is a better one!
Re: Text string bug
Mark.
Craig
How does it do numeric with mixed strings?(it only does numeric if the types of the two sides are mixed);
Craig
Re: Text string bug
Is the issue not with the presence of quotes rather than a discussion of types?
I think most would consider anything wrapped in quotes to be a string, or to be treated like a string. Prefixing it with a text character is a kludge but amounts to the same semantic effort...
I think most would consider anything wrapped in quotes to be a string, or to be treated like a string. Prefixing it with a text character is a kludge but amounts to the same semantic effort...
Re: Text string bug
@stam: I don't see the distinction - you are wanting quotes to indicate type.
As my previous example was meant to illustrate, I don't see how you can have "100" act differently from item 2 of "0,100,200" - one is a string literal, the other is a substring of a string literal.
You argue that as there are no quotes around 100 in item 2 of "0,100,200" so the difference all makes sense - but then by the same logic, shoudn't "100" be the string QUOTE 100 QUOTE (otherwise there's a huge inconsistency there!).
At the end of the day xTalks do not generally make a distinction between numbers and numeric strings (i.e. strings which are valid decimal numbers in some notation or another). The usage of quotes in the syntax is to ensure you can express things which otherwise would be understood as syntax (i.e. things with spaces in it, or things which are keywords).
The upside of this is that in virtually all cases you don't have to explicitly indicate whether something is a string or a number to get a desired effect - however this thread is entirely down to the one of the cases where you *do* have to - because the comparison operators are designed to not make a distinction, and thus cause a problem when you don't actually want to do numeric comparison. (So the only upshot of this thread really is that LC perhaps needs a set 'please compare these two things as strings, and only strings, operators').
As my previous example was meant to illustrate, I don't see how you can have "100" act differently from item 2 of "0,100,200" - one is a string literal, the other is a substring of a string literal.
You argue that as there are no quotes around 100 in item 2 of "0,100,200" so the difference all makes sense - but then by the same logic, shoudn't "100" be the string QUOTE 100 QUOTE (otherwise there's a huge inconsistency there!).
At the end of the day xTalks do not generally make a distinction between numbers and numeric strings (i.e. strings which are valid decimal numbers in some notation or another). The usage of quotes in the syntax is to ensure you can express things which otherwise would be understood as syntax (i.e. things with spaces in it, or things which are keywords).
The upside of this is that in virtually all cases you don't have to explicitly indicate whether something is a string or a number to get a desired effect - however this thread is entirely down to the one of the cases where you *do* have to - because the comparison operators are designed to not make a distinction, and thus cause a problem when you don't actually want to do numeric comparison. (So the only upshot of this thread really is that LC perhaps needs a set 'please compare these two things as strings, and only strings, operators').
-
- VIP Livecode Opensource Backer
- Posts: 10043
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Text string bug
Would optional type declarations be a feasible candidate for solution to this need?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Text string bug
@Mark: In truth i've not had an issue like this, so i'm guessing this is not a common problem - but playing devil's advocate:
Semantically i would argue that "100,200,300" can be viewed as both a string but also as a container, analogous to a numerically indexed array in other languages.
By using 'item of' you are implicitly declaring/treating this as a container/1-based array and not a string - as such there is no inconsistency.
Still not sure i see what the problem with using quotes to denote a string is - ie a scenario in which this would have a negative impact?
Stam
Re: Text string bug
So "100,200,300" can be viewed as a string 'and something else' - but "100" can only be viewed as a string? That seems a little inconsistentSemantically i would argue that "100,200,300" can be viewed as both a string but also as a container, analogous to a numerically indexed array in other languages.

By using 'is' you are indicating you want to see if the two values on either side are the same from any point of view - whether that be as strings or as numbers.By using 'item of' you are implicitly declaring/treating this as a container/1-based array and not a string - as such there is no inconsistency.
My point here is that LC generally defines what happens by the operations you perform, and not what the representation of the things being operated on are.
Code: Select all
Still not sure i see what the problem with using quotes to denote a string is - ie a scenario in which this would have a negative impact?
I do actually think the comparison operators are the real especially 'irksome' case here - after all arithmetic operations need numbers, so the not caring has no negative side-effects.
However, the reason they do what they do is because if they did not then you would get discontinuity in manipulation of strings which give rise to numeric strings.
Consider:
Code: Select all
put "100" is 100
put ("1" & "00") is 100
put item 2 of "1,2,3" is 2
put "100" into tVar; put tVar is 100

Re: Text string bug
I think we can all live with this and agree about the tiny "inconsistencies" that may only mean anything with comparisons.
i want to ask again, though, about JavaScript:
Mark wrote:
Craig
i want to ask again, though, about JavaScript:
Mark wrote:
How does it do numeric with mixed strings?it only does numeric if the types of the two sides are mixed
Craig
Re: Text string bug
It potentially could, but I think that would be a bit of a sledgehammer in this caseWould optional type declarations be a feasible candidate for solution to this need?

One quite xTalky way of resolving this issue is through string-only comparison operators - as its in keeping with the 'types don't matter, but what you ask to be done does' philosophy which seems to have implicitly underlined the evolution of this type of language.
To resolve the issue with the 'swiss army knife' is, for example, simply requires being able to state what 'type' you want to compare things as. e.g.
Code: Select all
put "100" is string 100 -- true
put 0 is string "0E0" -- false
put "a" is number "a" -- false (its an open question whether this should actually throw an error or not!)
put "100" is number "1.0e2" -- true
Code: Select all
put "20" < string "3" -- true
put "20" < number 3 -- false
Code: Select all
set the compareNumeric to false
put "000" is "0E0" -- false
set the compareNumeric to true
put "000" is "0E0" -- true
Re: Text string bug
Heh - I didn't write that very well - JavaScript attempts to compare numerically if one side is a number. If both sides are strings then it compares as strings.How does it do numeric with mixed strings?
So, in that sense, JS does not have this string comparison issue ("000" == "0e0" is false in JS) *however* the balance to that is that if you want something you've cut out of a string to behave numerically you pretty much always have to explicitly convert it.
Re: Text string bug
@Mark.
This is so much more fun for me than all that stuff about databases.
Craig
OK, thanks.JavaScript attempts to compare numerically if one side is a number. If both sides are strings then it compares as strings.
This is so much more fun for me than all that stuff about databases.
Craig
Re: Text string bug
I had my brain wrenched in the thread that I linked to, and could not see how anything quoted should be interpreted as anything but a string literal.
Following that thread and the linked quality report and subsequently, I have somehow come to understand that it isn't so black and white.
I don't know what the "best" answer is, but I suspect somehow that letting go of the string literal only interpretation is involved.
I am not sure I like it, and I am certainly not used to it, but I can reconcile it a bit with allusion to boolean comparison as in "some text" resolving to true and 0 resolving to false.
I am a bit more sold on havingresolving properly
Following that thread and the linked quality report and subsequently, I have somehow come to understand that it isn't so black and white.
I don't know what the "best" answer is, but I suspect somehow that letting go of the string literal only interpretation is involved.
I am not sure I like it, and I am certainly not used to it, but I can reconcile it a bit with allusion to boolean comparison as in "some text" resolving to true and 0 resolving to false.
I am a bit more sold on having
Code: Select all
put "100101" into tString
if char 1 to 3 of tString < char 4 to 6 of tString then ...
Re: Text string bug
Thanks for the explanations @LCMark - while i haven't so far been troubled by this, i, like others here, assumed quotes would cast the value as a string but good to know that assumption is completely wrong 

Re: Text string bug
I just learned a whole lot.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: Text string bug
Ha. Back when we were open-source I actually had this partially coded in the engine.Would optional type declarations be a feasible candidate for solution to this need?
But that was then.
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev