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
why are these not the same?
-
- Regular
- Posts: 315
- Joined: 02 Nov 2013 11:19
why are these not the same?
Last edited by oldgearguy on 22 Jan 2015 10:48, edited 2 times in total.
Re: why are these not the same?
Shouldn't it be
(x == 1) ? doIt(1) : doIt(0);
Or am I confusing this with a different formula?
(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
https://soundcloud.com/mrcorba
Re: why are these not the same?
I think Mr Corba nailed it!
-
- Regular
- Posts: 315
- Joined: 02 Nov 2013 11:19
Re: why are these not the same?
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.
I attached a simple test script here to demonstrate.
- Attachments
-
- test.jzml
- ternary versus if...else
- (3.2 KiB) Downloaded 52 times
-
- Regular
- Posts: 315
- Joined: 02 Nov 2013 11:19
Re: why are these not the same?
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');
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?
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);
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
https://soundcloud.com/mrcorba
Re: why are these not the same?
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.
(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.
-
- Regular
- Posts: 315
- Joined: 02 Nov 2013 11:19
Re: why are these not the same?
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.
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.
-
- Regular
- Posts: 315
- Joined: 02 Nov 2013 11:19
Re: why are these not the same?
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.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.