discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

IF IS AN ERROR here

GH
gene heskett
Sun, Mar 27, 2022 6:45 AM

Greetings all;
OpenSCAD-2022-02-26.

Can someone explain to me how to set a var dependent on a statement of
the form:

if(mode==numeric value) var=1 else var=calculated value;

All I can get is a syntax error pointing at the else* line. So what are
the rules for the else clause?

Cheers, Gene Heskett.

"There are four boxes to be used in defense of liberty:
soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author, 1940)
If we desire respect for the law, we must first make the law respectable.

  • Louis D. Brandeis
Greetings all; OpenSCAD-2022-02-26. Can someone explain to me how to set a var dependent on a statement of the form: if(mode==numeric value) var=1 else var=calculated value; All I can get is a syntax error pointing at the else* line. So what are the rules for the else clause? Cheers, Gene Heskett. -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author, 1940) If we desire respect for the law, we must first make the law respectable. - Louis D. Brandeis
MF
Michael Frey
Sun, Mar 27, 2022 7:08 AM

On 27.03.22 08:45, gene heskett wrote:

Greetings all;
OpenSCAD-2022-02-26.

Welcome :-)

Can someone explain to me how to set a var dependent on a statement of
the form:

if(mode==numeric value) var=1 else var=calculated value;

All I can get is a syntax error pointing at the else* line. So what are
the rules for the else clause?

I would not use if.

I would use ?.

Assuming something like

if(mode=="numeric value") var=1 else var=calculated;

I would change it to

var = (mode=="numeric value") ? 1 : calculated;

If I remember correctly, this has something to do with variable scope.

If I would rewrite your code to

if(mode=="numeric value"){
    var=1;
}else{
    var=calculated;
}

it would not work, has {} creates a new variable Scope.

Also assuming to want to write short code, ? : is shorter then if else

https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Variables

With Kind regards,

Michael Frey

On 27.03.22 08:45, gene heskett wrote: > Greetings all; > OpenSCAD-2022-02-26. Welcome :-) > > Can someone explain to me how to set a var dependent on a statement of > the form: > > if(mode==numeric value) var=1 else var=calculated value; > > All I can get is a syntax error pointing at the else* line. So what are > the rules for the else clause? I would not use if. I would use ?. Assuming something like if(mode=="numeric value") var=1 else var=calculated; I would change it to var = (mode=="numeric value") ? 1 : calculated; If I remember correctly, this has something to do with variable scope. If I would rewrite your code to if(mode=="numeric value"){     var=1; }else{     var=calculated; } it would not work, has {} creates a new variable Scope. Also assuming to want to write short code, ? : is shorter then if else https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Variables With Kind regards, Michael Frey
GH
gene heskett
Sun, Mar 27, 2022 10:28 AM

On Sunday, 27 March 2022 03:08:44 EDT Michael Frey wrote:

On 27.03.22 08:45, gene heskett wrote:

Greetings all;
OpenSCAD-2022-02-26.

Welcome :-)

Can someone explain to me how to set a var dependent on a statement
of

the form:

if(mode==numeric value) var=1 else var=calculated value;

All I can get is a syntax error pointing at the else* line. So what
are

the rules for the else clause?

I would not use if.

I would use ?.

Assuming something like

if(mode=="numeric value") var=1 else var=calculated;

I would change it to

var = (mode=="numeric value") ? 1 : calculated;

Which does work nicely once I got the conditions correct. But it
discloses a rather worrysome bit of interference too, at points just a
few degrees from full engagement. What I am doing is simulating an xray
view of the internals of a 40/1 harmonic drive by showing the assembled
distortions that make it work.
pix from your png generator attached, showing the spline engaements of an
80 tooth loose belt, working against an 80 tooth internal spline and in
the next one, and 82 tooth internal

If I remember correctly, this has something to do with variable scope.

yes, I had to move several vars to the outside scope of the whole thing
where they were available to all modules. This whole drive is now 950
LOC, and some modules take over 5 minutes to render to and .stl.

If I would rewrite your code to

if(mode=="numeric value"){
var=1;
}else{
var=calculated;
}

it would not work, has {} creates a new variable Scope.

Also assuming to want to write short code, ? : is shorter then if else

https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Variables

With Kind regards,

Michael Frey

Thank you Michael Frey.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Cheers, Gene Heskett.

"There are four boxes to be used in defense of liberty:
soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author, 1940)
If we desire respect for the law, we must first make the law respectable.

  • Louis D. Brandeis
On Sunday, 27 March 2022 03:08:44 EDT Michael Frey wrote: > On 27.03.22 08:45, gene heskett wrote: > > > Greetings all; > > OpenSCAD-2022-02-26. > > Welcome :-) > > > > > > > Can someone explain to me how to set a var dependent on a statement > > of the form: > > > > > > > > if(mode==numeric value) var=1 else var=calculated value; > > > > > > > > All I can get is a syntax error pointing at the else* line. So what > > are the rules for the else clause? > > > I would not use if. > > I would use ?. > > Assuming something like > > if(mode=="numeric value") var=1 else var=calculated; > > > I would change it to > > var = (mode=="numeric value") ? 1 : calculated; Which does work nicely once I got the conditions correct. But it discloses a rather worrysome bit of interference too, at points just a few degrees from full engagement. What I am doing is simulating an xray view of the internals of a 40/1 harmonic drive by showing the assembled distortions that make it work. pix from your png generator attached, showing the spline engaements of an 80 tooth loose belt, working against an 80 tooth internal spline and in the next one, and 82 tooth internal > > If I remember correctly, this has something to do with variable scope. yes, I had to move several vars to the outside scope of the whole thing where they were available to all modules. This whole drive is now 950 LOC, and some modules take over 5 minutes to render to and .stl. > > > If I would rewrite your code to > > if(mode=="numeric value"){ > var=1; > }else{ > var=calculated; > } > > it would not work, has {} creates a new variable Scope. > > Also assuming to want to write short code, ? : is shorter then if else > > > https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Variables > > > With Kind regards, > > Michael Frey Thank you Michael Frey. > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org Cheers, Gene Heskett. -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author, 1940) If we desire respect for the law, we must first make the law respectable. - Louis D. Brandeis
GH
gene heskett
Sun, Mar 27, 2022 5:34 PM

On Sunday, 27 March 2022 06:28:12 EDT gene heskett wrote:

On Sunday, 27 March 2022 03:08:44 EDT Michael Frey wrote:

On 27.03.22 08:45, gene heskett wrote:

[...]

I would change it to

var = (mode=="numeric value") ? 1 : calculated;
Michael Frey

Thank you Michael Frey.

However, in order to get the correct behaviour, I had to remove the
dblquotes around the comparison value, it was not a text, it was
numerical.  The text comparison couldn't make up its mind consistently.

Thanks again Michael Frey.

Cheers, Gene Heskett.

"There are four boxes to be used in defense of liberty:
soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author, 1940)
If we desire respect for the law, we must first make the law respectable.

  • Louis D. Brandeis
On Sunday, 27 March 2022 06:28:12 EDT gene heskett wrote: > On Sunday, 27 March 2022 03:08:44 EDT Michael Frey wrote: > > On 27.03.22 08:45, gene heskett wrote: [...] > > > > I would change it to > > > > var = (mode=="numeric value") ? 1 : calculated; > > Michael Frey > > Thank you Michael Frey. However, in order to get the correct behaviour, I had to remove the dblquotes around the comparison value, it was not a text, it was numerical. The text comparison couldn't make up its mind consistently. Thanks again Michael Frey. Cheers, Gene Heskett. -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author, 1940) If we desire respect for the law, we must first make the law respectable. - Louis D. Brandeis