Page 1 of 1

why are these not the same?

Posted: 22 Jan 2015 01:02
by oldgearguy
Original post edited -- I typed too quickly...


script attached to a switch, set for On Expression, x, any


(x == 1) ? doIt(1) : doIt(0);

never triggers the true case

if (x == 1)
doIt(1);
else
doIt(0);

works as designed

Re: why are these not the same?

Posted: 22 Jan 2015 01:22
by MrCorba
Shouldn't it be

(x == 1) ? doIt(1) : doIt(0);

Or am I confusing this with a different formula?

Re: why are these not the same?

Posted: 22 Jan 2015 07:56
by Softcore
I think Mr Corba nailed it!

Re: why are these not the same?

Posted: 22 Jan 2015 10:43
by oldgearguy
I edited the original -- typed too quickly. Yes, the '?' is/was in the statement in my code.


I attached a simple test script here to demonstrate.

Re: why are these not the same?

Posted: 22 Jan 2015 11:49
by oldgearguy
and while I'm here ranting,

why does this work -
setattribute(Monitor, 'label', 1); // use 1 to show name, 0 to hide it

and this does not????
setattribute(Monitor, 'name', 'New Name');

Re: why are these not the same?

Posted: 22 Jan 2015 11:54
by MrCorba
I think I found a fix for your first problem, don't know exactly why but this does seem to work:

doIt( (x == 1) ? 1 : 0);

Re: why are these not the same?

Posted: 22 Jan 2015 12:49
by Softcore
Yup the doIt part doesnt work although I think I agree it should. mr Corba's syntax is indeed more elegant, but still I cant find why:

(x==1)? doIt(1) : doIt(0);

shouldnt work. Perhaps the a?b:c syntax comes from the old days of one line scripting of Lemur and as such, wasnt designed to "call" custom functions? Dont have a clue

As for the name attribute, I dont know if its by design, but I just noticed that in the Lemur user guide, the description for the name attribute says:

get object name.

Perhaps this attribute is only meant to be "read" and not changed? Not sure! I have never tried to rename an object via scripting before.

Re: why are these not the same?

Posted: 22 Jan 2015 14:26
by oldgearguy
Thanks for confirming I'm not going crazy. I also tried assigning the result to a dummy variable (dummy = (test) ? choice(A) : choice (B);) to see if forcing it to store the result would have an effect, but that didn't change the behavior.

I agree, the test embedded in the functional call is more elegant and that is what I'm generally going to do. It get a little less clear when the 'stuff' being evaluated to the left of the question mark and the choices on the right are more complex expressions than the example, but it's good to know it works.

Since scripts have size limits, any way to conserve characters is welcome.

Re: why are these not the same?

Posted: 22 Jan 2015 14:30
by oldgearguy
Softcore wrote:Yup the doIt part doesnt work although I think I agree it should. mr Corba's syntax is indeed more elegant, but still I cant find why:

(x==1)? doIt(1) : doIt(0);

shouldnt work. Perhaps the a?b:c syntax comes from the old days of one line scripting of Lemur and as such, wasnt designed to "call" custom functions? Dont have a clue

As for the name attribute, I dont know if its by design, but I just noticed that in the Lemur user guide, the description for the name attribute says:

get object name.

Perhaps this attribute is only meant to be "read" and not changed? Not sure! I have never tried to rename an object via scripting before.
The main reason for it was to use a single Monitor object as a general debug display module. Assign the name as the name of the object being moved and then display the value on the second line. That way I could encapsulate a "setattribute() and Monitor.value =" combo in a script and call it from wherever I needed to see what was going on.