Page 1 of 2

Odd if statement

Posted: Tue Feb 19, 2013 4:50 am
by dunbarx
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

Re: Odd if statement

Posted: Mon Mar 11, 2013 12:25 pm
by Mark
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

Re: Odd if statement

Posted: Mon Mar 11, 2013 1:51 pm
by dunbarx
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

Re: Odd if statement

Posted: Mon Mar 11, 2013 2:58 pm
by Mark
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

Re: Odd if statement

Posted: Mon Mar 11, 2013 5:25 pm
by mwieder
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...

Re: Odd if statement

Posted: Mon Mar 11, 2013 5:35 pm
by Mark
Mark,

That's what I said.

Mark

Re: Odd if statement

Posted: Mon Mar 11, 2013 7:24 pm
by mwieder
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

Re: Odd if statement

Posted: Mon Mar 11, 2013 7:32 pm
by Mark
Mark,

Now you're wrong.

Best,

Mark

Re: Odd if statement

Posted: Mon Mar 11, 2013 9:11 pm
by mwieder
Really? You get something other than "false"?

Re: Odd if statement

Posted: Mon Mar 11, 2013 9:20 pm
by Mark
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

Re: Odd if statement

Posted: Mon Mar 11, 2013 10:12 pm
by mwieder
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.

Re: Odd if statement

Posted: Mon Mar 11, 2013 10:20 pm
by Mark
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

Re: Odd if statement

Posted: Tue Mar 12, 2013 1:33 am
by sturgis
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

Re: Odd if statement

Posted: Tue Mar 12, 2013 2:30 am
by Mark
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

Re: Odd if statement

Posted: Tue Mar 12, 2013 2:35 am
by sturgis
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