why are these not the same?

Discuss problems and solutions.
Post Reply
oldgearguy
Regular
Posts: 315
Joined: 02 Nov 2013 11:19

why are these not the same?

Post 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
Last edited by oldgearguy on 22 Jan 2015 10:48, edited 2 times in total.
MrCorba
Regular
Posts: 142
Joined: 02 Sep 2013 20:17
Location: Netherlands

Re: why are these not the same?

Post by MrCorba »

Shouldn't it be

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

Or am I confusing this with a different formula?
"Having no silence in music is like having no black or white in a painting" - Brian Eno
https://soundcloud.com/mrcorba
Softcore
Regular
Posts: 1613
Joined: 04 Nov 2012 08:34

Re: why are these not the same?

Post by Softcore »

I think Mr Corba nailed it!
oldgearguy
Regular
Posts: 315
Joined: 02 Nov 2013 11:19

Re: why are these not the same?

Post 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.
Attachments
test.jzml
ternary versus if...else
(3.2 KiB) Downloaded 51 times
oldgearguy
Regular
Posts: 315
Joined: 02 Nov 2013 11:19

Re: why are these not the same?

Post 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');
MrCorba
Regular
Posts: 142
Joined: 02 Sep 2013 20:17
Location: Netherlands

Re: why are these not the same?

Post 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);
"Having no silence in music is like having no black or white in a painting" - Brian Eno
https://soundcloud.com/mrcorba
Softcore
Regular
Posts: 1613
Joined: 04 Nov 2012 08:34

Re: why are these not the same?

Post 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.
oldgearguy
Regular
Posts: 315
Joined: 02 Nov 2013 11:19

Re: why are these not the same?

Post 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.
oldgearguy
Regular
Posts: 315
Joined: 02 Nov 2013 11:19

Re: why are these not the same?

Post 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.
Post Reply