Text string bug

If you find an issue in LiveCode but are having difficulty pinning down a reliable recipe or want to sanity-check your findings with others, this is the place.

Please have one thread per issue, and try to summarize the issue concisely in the thread title so others can find related issues here.

Moderator: Klaus

Andy01
Posts: 14
Joined: Wed Dec 22, 2021 3:26 am

Text string bug

Post by Andy01 » Wed Dec 22, 2021 4:01 am

Im checking if character 1 to 3 of text string = 000 it returns true for the following sets of 3 characters 0E0 0E1 0E2 0E3 0E4 0E5 0E6 0E7 0E8 0E9
Example: put "0E0" = "000' returns true - this should return false

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Text string bug

Post by jmburnod » Wed Dec 22, 2021 9:19 am

Hi,
Welcome to this forum
Congratulations !
I confirm this strange result
Best regards
Jean-Marc
https://alternatic.ch

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1232
Joined: Thu Apr 11, 2013 11:27 am

Re: Text string bug

Post by LCMark » Wed Dec 22, 2021 10:54 am

@Andy01: This isn't a bug - its a side-effect of the fact that comparison operators in LiveCode try to compare numerically and *then* as strings.

LiveCode understands exponential notation of numbers - so 0E0, 0E1, ... are all numbers - 0*10^0, 0*10^1, ... - all of which are zero.

LiveCode also allows arbitrary numbers of 0 digits to prefix a number - so 000 is also seen as zero.

As both sides are valid numbers, LiveCode will compare numerically and thus return true.

One way to ensure that you compare as strings is to prefix both strings with a non-numeric character when comparing - e.g.

Code: Select all

put ("_" & tLeft) is ("_" & tRight)
This will return false if tLeft is "000" and tRight is "0E0".

Andy01
Posts: 14
Joined: Wed Dec 22, 2021 3:26 am

Re: Text string bug

Post by Andy01 » Wed Dec 22, 2021 1:57 pm

Okay thanks, I see what's happening now👌 thats very interesting. Thank you for showing me an example I was going to get around it by converting the 3 characters to ascii numbers but your example is heaps better. Cheers

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7389
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Text string bug

Post by jacque » Wed Dec 22, 2021 6:36 pm

I think it would also work if you put the strings in quotes, which forces a string comparison.

put "000" = "0E0"
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

Re: Text string bug

Post by SparkOut » Wed Dec 22, 2021 8:04 pm

No, that's the reason for the confusion.

FWIW I think quoted strings should be compared as, well, "strings". But I see where this differs from the engine comparison.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7389
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Text string bug

Post by jacque » Wed Dec 22, 2021 9:04 pm

I'm at the computer now and you're right, is this the only instance where a quoted string isn't a string? That's pretty odd.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

Re: Text string bug

Post by SparkOut » Wed Dec 22, 2021 9:10 pm


dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Text string bug

Post by dunbarx » Wed Dec 22, 2021 10:27 pm

LCMArk said:
As both sides are valid numbers, LiveCode will compare numerically and thus return true.
"Compare numerically", eh? So the LC parser takes what it "sees" as numeric "strings" and ignores the quotes.

Thanks a lot, LC parser. I think that quotes around ANYTHING ought to be considered just as they appear, no strings attached. I would say that LC ought to transform the chars into their ASCII values and THEN see if they match.

I think this is, if not a bug, at the least poor thinking on the part of LC.

Craig

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1232
Joined: Thu Apr 11, 2013 11:27 am

Re: Text string bug

Post by LCMark » Thu Dec 23, 2021 7:39 am

dunbarx wrote:
Wed Dec 22, 2021 10:27 pm
I think this is, if not a bug, at the least poor thinking on the part of LC.
Hmmm - so you'd prefer if:

Code: Select all

put "100,200,300" into tList
if item 2 of tList < 300 then
    add 100 to item 2 of tList
end if
Didn't work and threw an error?

i.e. You had to explicitly say whether you wanted a string to be used as a number wherever you do?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Text string bug

Post by dunbarx » Thu Dec 23, 2021 2:53 pm

Mark.

But when the "if" line is executed don't we have the simple three char string 300 (I left off the quotes for simple snarkiness :D ) essentially unquoted? In other words, the 300 is extracted naked from the list of items, even though those items were initially presented as a quoted string. It lives clearly as an unquoted string in that step.

In even different words, to show I appreciate the slippery slope, it seems reasonable to me that both of these lines beep:

Code: Select all

if "42" is a number then beep
if 42 is a number then beep
LC evaluates the quoted number in line 1 into a numerical string in order to decide to beep

Perhaps it is not fair to the parser to complain only in the case where comparisons are being made? But your kludge, to add an extra character to make a numerical string non-numerical in order to bypass the way things work seems to me untoward.

Craig

stam
Posts: 3061
Joined: Sun Jun 04, 2006 9:39 pm

Re: Text string bug

Post by stam » Thu Dec 23, 2021 3:38 pm

LCMark wrote:
Thu Dec 23, 2021 7:39 am
dunbarx wrote:
Wed Dec 22, 2021 10:27 pm
I think this is, if not a bug, at the least poor thinking on the part of LC.
Hmmm - so you'd prefer if:

Code: Select all

put "100,200,300" into tList
if item 2 of tList < 300 then
    add 100 to item 2 of tList
end if
Didn't work and threw an error?

i.e. You had to explicitly say whether you wanted a string to be used as a number wherever you do?
Forgive me if i misunderstand, but even though tList is a string enclosed by quotes, item 2 of tList is not. In your example, this would be 200, not "200" and by that logic the code would work even if quotes denote a string.

I personally find it difficult having numbers in quotes being considered a number and not a string, as quotes denote text no matter what environment pretty much universally outside of LC.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Text string bug

Post by FourthWorld » Thu Dec 23, 2021 4:34 pm

stam wrote:
Thu Dec 23, 2021 3:38 pm
I personally find it difficult having numbers in quotes being considered a number and not a string, as quotes denote text no matter what environment pretty much universally outside of LC.
And just like that xTalk fans began to appreciate declared data types...
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Text string bug

Post by dunbarx » Thu Dec 23, 2021 4:38 pm

And just like that xTalk fans began to appreciate declared data types...
Richard. :wink:

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Text string bug

Post by dunbarx » Thu Dec 23, 2021 4:47 pm

I personally find it difficult having numbers in quotes being considered a number and not a string, as quotes denote text no matter what environment pretty much universally outside of LC.
If so, to be consistent, whatever that means, in the code:

Code: Select all

if "42" is a number then beep
if 42 is a number then beep
Line 1 really ought not to beep. That would solve all issues, no? Anything in quotes is just a string. LC need not declare variable types, (whew) and all is well.

The issue is that xTalks are, at heart, based on the engine resolving stuff, sometimes a couple of levels down, before executing that stuff. We have been nurtured on that paradigm since 1987, and cannot live any other way.

In that sense, one just has to be careful with "0E0". And maybe that is a special case that does not rear its head anywhere else?

Craig

Post Reply