Odd if statement

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

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

Odd if statement

Post by dunbarx » Tue Feb 19, 2013 4:50 am

Saw this incidentally in another thread.

This statement throws no error;

if x,y,z = 0 then...

I found it odd that the LC compiler lets this pass. When LC resolves the string, it has to come back as "x,y,z", Does LC mean to say that such a string is, simply, not 0?

I would have bet two dollars this would have been rejected on general principles and righteousness.

Craig Newman

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Odd if statement

Post by Mark » Mon Mar 11, 2013 12:25 pm

Hi Craig,

This is just a special case of how xTalk languages parse strings. The given string is a comma-delimited list. LiveCode first parses each item in the list, continues to parse the list as a whole and eventually considers the list to be a string when it parses the remainder of the syntax on the line. Try this in the message box:

Code: Select all

put 2*2,3*3,0=0
If you want to parse the equation as a separate item of the list, you have to write

Code: Select all

put 2*2,3*3,(0=0)
Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

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

Re: Odd if statement

Post by dunbarx » Mon Mar 11, 2013 1:51 pm

Mark.

Guess I am getting old. HC throws an error with any such construction. But LC does indeed give answers to such a string. I intend to ignore this feature.

Craig

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Odd if statement

Post by Mark » Mon Mar 11, 2013 2:58 pm

Hi Craig,

I checked SuperCard and SC throws an error too, just like HyperCard. One might argue that this is wrong, but one might also consider it a relatively new development in xTalk language. Pick you choice

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Odd if statement

Post by mwieder » Mon Mar 11, 2013 5:25 pm

I could be wrong, but I believe the parser's interpretation of
if x,y,z = 0 then...
is

Code: Select all

if "x,y,z" = "0" then...

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Odd if statement

Post by Mark » Mon Mar 11, 2013 5:35 pm

Mark,

That's what I said.

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Odd if statement

Post by mwieder » Mon Mar 11, 2013 7:24 pm

Yes, you just didn't explain it well enough to make it readily understandable.

Code: Select all

put 2*2,3*3,0=0
is parsed as

Code: Select all

put "2*2,3*3,0"=0

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Odd if statement

Post by Mark » Mon Mar 11, 2013 7:32 pm

Mark,

Now you're wrong.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Odd if statement

Post by mwieder » Mon Mar 11, 2013 9:11 pm

Really? You get something other than "false"?

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Odd if statement

Post by Mark » Mon Mar 11, 2013 9:20 pm

Mark,

No, but you forget that the items in the list are parsed before the list as a whole. Try this:

Code: Select all

put 2*2,3*3,(0=0) --> 4,9,true
put 2*2,3*3,0=0 --> false
This implies that 2*2,3*3,0=0 isn't parsed as "2*2,3*3,0"=0 but as "4,9,0"=0.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Odd if statement

Post by mwieder » Mon Mar 11, 2013 10:12 pm

Ah, OK - I see what you're saying now. But it's irrelevant.
The important thing is that there's an rValue and an lValue in the equation, and no matter how the the string in the lValue is evaluated it's never going to satisfy the equality statement unless it really evaluates to zero. And no string with embedded commas is going to do that.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Odd if statement

Post by Mark » Mon Mar 11, 2013 10:20 pm

Hi,

It is true that a comma delimited list with more than 1 item will never evaluate to 0 and therefore the equation in the example will never return true, but what about

put 2*2,3*3=4,9

Now, it is suddenly very relevant. Another very interesting feature is the possibility to use functions in the lists on both sides of the equal sign, which allows for testing complex conditions.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Odd if statement

Post by sturgis » Tue Mar 12, 2013 1:33 am

Hmm. Your example evaluates to true, changing the first 2*2 to something else makes it false, my question is.. WHY? This one has me royally stumped, would appreciate an explanation of whats going on behind the scenes so I can get my noggin around this one.
Mark wrote:Hi,

It is true that a comma delimited list with more than 1 item will never evaluate to 0 and therefore the equation in the example will never return true, but what about

put 2*2,3*3=4,9

Now, it is suddenly very relevant. Another very interesting feature is the possibility to use functions in the lists on both sides of the equal sign, which allows for testing complex conditions.

Kind regards,

Mark

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Odd if statement

Post by Mark » Tue Mar 12, 2013 2:30 am

Hi,

LiveCode first parses the items in the list on the left side of the equal sign. The second step is to parse the items in the list on the right side. Third, the list on the left side is parsed as a string and the fourth step is to parse the list on the right side as a string, after which the two strings are compared.

Best regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Odd if statement

Post by sturgis » Tue Mar 12, 2013 2:35 am

Ah k, I see it now. left side of the = turns into 4,9, right side is already 4,9. Dunno why I was making it so complicated. Thanks!

2*2,3*3=4,9

Post Reply