discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Puzzled here

E
Experimentalist
Sat, Apr 23, 2016 3:13 PM

Hi

I am sure I must be doing something very silly here but I just can't spot
it. Here is some test code to demonstrate my issue:

Test(choice = 1);
// Test(choice = 2);
// Test(choice = 3);

module Test(choice = 1){
echo(choice=choice);
myVar = 0;

if (choice==1) {
    myVar = 1;
} else if (choice == 2) {
    myVar = 2;
} else {
     myVar = 3;
}

echo(myVar=myVar);

}
<<<

Why is myVar always 0 at the echo statement?

Hope you can help
Ex

--
View this message in context: http://forum.openscad.org/Puzzled-here-tp17172.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Hi I am sure I must be doing something very silly here but I just can't spot it. Here is some test code to demonstrate my issue: >>> Test(choice = 1); // Test(choice = 2); // Test(choice = 3); module Test(choice = 1){ echo(choice=choice); myVar = 0; if (choice==1) { myVar = 1; } else if (choice == 2) { myVar = 2; } else { myVar = 3; } echo(myVar=myVar); } <<< Why is myVar always 0 at the echo statement? Hope you can help Ex -- View this message in context: http://forum.openscad.org/Puzzled-here-tp17172.html Sent from the OpenSCAD mailing list archive at Nabble.com.
DM
doug moen
Sat, Apr 23, 2016 3:45 PM

OpenSCAD is a declarative language. It doesn't have mutable variables (or
mutable data structures). Each variable is defined in exactly one location.
It does have nested scopes, and {...} introduces a new scope, which is why
you don't get an error.

You can't use multiple assignment statements to specify the value of a
variable.

Try this:
myVar =
(choice==1) ?
1
: (choice == 2) ?
2
:
3;

On 23 April 2016 at 11:13, Experimentalist experimentalist@aaawesome.co.uk
wrote:

Hi

I am sure I must be doing something very silly here but I just can't spot
it. Here is some test code to demonstrate my issue:

Test(choice = 1);
// Test(choice = 2);
// Test(choice = 3);

module Test(choice = 1){
echo(choice=choice);
myVar = 0;

 if (choice==1) {
     myVar = 1;
 } else if (choice == 2) {
     myVar = 2;
 } else {
      myVar = 3;
 }

 echo(myVar=myVar);

}
<<<

Why is myVar always 0 at the echo statement?

Hope you can help
Ex

--
View this message in context:
http://forum.openscad.org/Puzzled-here-tp17172.html
Sent from the OpenSCAD mailing list archive at Nabble.com.


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

OpenSCAD is a declarative language. It doesn't have mutable variables (or mutable data structures). Each variable is defined in exactly one location. It does have nested scopes, and {...} introduces a new scope, which is why you don't get an error. You can't use multiple assignment statements to specify the value of a variable. Try this: myVar = (choice==1) ? 1 : (choice == 2) ? 2 : 3; On 23 April 2016 at 11:13, Experimentalist <experimentalist@aaawesome.co.uk> wrote: > Hi > > I am sure I must be doing something very silly here but I just can't spot > it. Here is some test code to demonstrate my issue: > > >>> > Test(choice = 1); > // Test(choice = 2); > // Test(choice = 3); > > module Test(choice = 1){ > echo(choice=choice); > myVar = 0; > > if (choice==1) { > myVar = 1; > } else if (choice == 2) { > myVar = 2; > } else { > myVar = 3; > } > > echo(myVar=myVar); > } > <<< > > Why is myVar always 0 at the echo statement? > > Hope you can help > Ex > > > > > > -- > View this message in context: > http://forum.openscad.org/Puzzled-here-tp17172.html > Sent from the OpenSCAD mailing list archive at Nabble.com. > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org > > >
W
wolf
Sat, Apr 23, 2016 9:34 PM

You can reassign values to variables. This shows you how:
Test(choice = 1);
// Test(choice = 2);
// Test(choice = 3);

module Test(choice = 1){
echo(choice=choice);
//  myVar = 0;

if (choice==1) {
    myVar=var(choice);
} else if (choice == 2) {
    myVar=var(choice);
} else {
     myVar=var(choice);
}

echo(myVar);

}
function var(choice)=choice;

wolf

--
View this message in context: http://forum.openscad.org/Puzzled-here-tp17172p17176.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

You can reassign values to variables. This shows you how: Test(choice = 1); // Test(choice = 2); // Test(choice = 3); module Test(choice = 1){ echo(choice=choice); // myVar = 0; if (choice==1) { myVar=var(choice); } else if (choice == 2) { myVar=var(choice); } else { myVar=var(choice); } echo(myVar); } function var(choice)=choice; wolf -- View this message in context: http://forum.openscad.org/Puzzled-here-tp17172p17176.html Sent from the OpenSCAD mailing list archive at Nabble.com.
NH
nop head
Sat, Apr 23, 2016 9:47 PM

ECHO: choice = 1

WARNING: Ignoring unknown variable 'myVar'.

ECHO: undef

What makes you think it works? Each myVar is a different variable because
brackets open a new scope. None of them are in scope when it gets to the
echo.

On 23 April 2016 at 22:34, wolf wv99999@gmail.com wrote:

You can reassign values to variables. This shows you how:
Test(choice = 1);
// Test(choice = 2);
// Test(choice = 3);

module Test(choice = 1){
echo(choice=choice);
//  myVar = 0;

 if (choice==1) {
     myVar=var(choice);
 } else if (choice == 2) {
     myVar=var(choice);
 } else {
      myVar=var(choice);
 }

 echo(myVar);

}
function var(choice)=choice;

wolf

--
View this message in context:
http://forum.openscad.org/Puzzled-here-tp17172p17176.html
Sent from the OpenSCAD mailing list archive at Nabble.com.


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

ECHO: choice = 1 WARNING: Ignoring unknown variable 'myVar'. ECHO: undef What makes you think it works? Each myVar is a different variable because brackets open a new scope. None of them are in scope when it gets to the echo. On 23 April 2016 at 22:34, wolf <wv99999@gmail.com> wrote: > You can reassign values to variables. This shows you how: > Test(choice = 1); > // Test(choice = 2); > // Test(choice = 3); > > module Test(choice = 1){ > echo(choice=choice); > // myVar = 0; > > if (choice==1) { > myVar=var(choice); > } else if (choice == 2) { > myVar=var(choice); > } else { > myVar=var(choice); > } > > echo(myVar); > } > function var(choice)=choice; > > > wolf > > > > -- > View this message in context: > http://forum.openscad.org/Puzzled-here-tp17172p17176.html > Sent from the OpenSCAD mailing list archive at Nabble.com. > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >
DM
doug moen
Sat, Apr 23, 2016 9:50 PM

Wolf, your program does not work, and contains errors. Here's the console
output that I get:

ECHO: choice = 1

WARNING: Ignoring unknown variable 'myVar'.

ECHO: undef

On 23 April 2016 at 17:34, wolf wv99999@gmail.com wrote:

You can reassign values to variables. This shows you how:
Test(choice = 1);
// Test(choice = 2);
// Test(choice = 3);

module Test(choice = 1){
echo(choice=choice);
//  myVar = 0;

 if (choice==1) {
     myVar=var(choice);
 } else if (choice == 2) {
     myVar=var(choice);
 } else {
      myVar=var(choice);
 }

 echo(myVar);

}
function var(choice)=choice;

wolf

--
View this message in context:
http://forum.openscad.org/Puzzled-here-tp17172p17176.html
Sent from the OpenSCAD mailing list archive at Nabble.com.


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

Wolf, your program does not work, and contains errors. Here's the console output that I get: ECHO: choice = 1 WARNING: Ignoring unknown variable 'myVar'. ECHO: undef On 23 April 2016 at 17:34, wolf <wv99999@gmail.com> wrote: > You can reassign values to variables. This shows you how: > Test(choice = 1); > // Test(choice = 2); > // Test(choice = 3); > > module Test(choice = 1){ > echo(choice=choice); > // myVar = 0; > > if (choice==1) { > myVar=var(choice); > } else if (choice == 2) { > myVar=var(choice); > } else { > myVar=var(choice); > } > > echo(myVar); > } > function var(choice)=choice; > > > wolf > > > > -- > View this message in context: > http://forum.openscad.org/Puzzled-here-tp17172p17176.html > Sent from the OpenSCAD mailing list archive at Nabble.com. > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org > > >
W
wolf
Sat, Apr 23, 2016 11:04 PM

Sorry, for a moment I did not remember that a module is intended to return a
shape, and a function to return a value. Thus, the confusion. What I should
have sent is this:

choice = 1;

echo(myVar());
Test(myVar());

module Test(xyz)
{
if      (xyz==1)      cube([10,xyz,xyz]);
else if  (xyz == 2)    cube([xyz,10,xyz]);
else if  (xyz == 3)    cube([xyz,xyz,10]);
else                  sphere(10);
}

function myVar()=choice==1?1:
choice==2?2:
choice==3?3:"not in list";

By changing the value assigned to choice, you may change the value reported
by echo() and the shape returned by Test(), which is what -I think-
experimentalist originally intended to do, when we were all mislead by
doug.moen's correct, but misleading answer.
I have read similar questions on these pages before, so the issue is not
new. To overcome it, may I suggest that above sample program be added to the
manual so that others in future can look up the answer there? In particular,
try to stress that module returns a shape, function a value!

wolf

--
View this message in context: http://forum.openscad.org/Puzzled-here-tp17172p17179.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Sorry, for a moment I did not remember that a module is intended to return a shape, and a function to return a value. Thus, the confusion. What I should have sent is this: choice = 1; echo(myVar()); Test(myVar()); module Test(xyz) { if (xyz==1) cube([10,xyz,xyz]); else if (xyz == 2) cube([xyz,10,xyz]); else if (xyz == 3) cube([xyz,xyz,10]); else sphere(10); } function myVar()=choice==1?1: choice==2?2: choice==3?3:"not in list"; By changing the value assigned to choice, you may change the value reported by echo() and the shape returned by Test(), which is what -I think- experimentalist originally intended to do, when we were all mislead by doug.moen's correct, but misleading answer. I have read similar questions on these pages before, so the issue is not new. To overcome it, may I suggest that above sample program be added to the manual so that others in future can look up the answer there? In particular, try to stress that module returns a shape, function a value! wolf -- View this message in context: http://forum.openscad.org/Puzzled-here-tp17172p17179.html Sent from the OpenSCAD mailing list archive at Nabble.com.
E
Experimentalist
Sun, Apr 24, 2016 8:21 AM

"what -I think- experimentalist originally intended to do"

What I was trying to do was change the Z offset of a translation in a module
by passing a more concise parameter to avoid code duplication and improve
readability. With your help I have added a function and achieved my goal,
thanks.

I never have, so maybe it is time to go RTFM.

--
View this message in context: http://forum.openscad.org/Puzzled-here-tp17172p17182.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

"what -I think- experimentalist originally intended to do" What I was trying to do was change the Z offset of a translation in a module by passing a more concise parameter to avoid code duplication and improve readability. With your help I have added a function and achieved my goal, thanks. I never have, so maybe it is time to go RTFM. -- View this message in context: http://forum.openscad.org/Puzzled-here-tp17172p17182.html Sent from the OpenSCAD mailing list archive at Nabble.com.
LB
L Boyd
Sun, Apr 24, 2016 10:32 PM

wolf wrote

Sorry, for a moment I did not remember that a module is intended to return
a shape, and a function to return a value. Thus, the confusion. What I
should have sent is this:

choice = 1;

echo(myVar());
Test(myVar());

module Test(xyz)
{
if      (xyz==1)      cube([10,xyz,xyz]);
else if  (xyz == 2)    cube([xyz,10,xyz]);
else if  (xyz == 3)    cube([xyz,xyz,10]);
else                  sphere(10);
}

function myVar()=choice==1?1:
choice==2?2:
choice==3?3:"not in list";

By changing the value assigned to choice, you may change the value
reported by echo() and the shape returned by Test(), which is what -I
think- experimentalist originally intended to do, when we were all mislead
by doug.moen's correct, but misleading answer.
I have read similar questions on these pages before, so the issue is not
new. To overcome it, may I suggest that above sample program be added to
the manual so that others in future can look up the answer there? In
particular, try to stress that module returns a shape, function a value!

wolf

Two comments.

  1. In OpenSCAD, modules create shapes but don't return anything.

  2. Your function myVar() returns a value which is a function of choice. This
    is what he original poster wanted.

Do not be mislead into thinking it reassigns a value for a variable.
Calling it several times while running the script will always return the
same value.


Larry

View this message in context: http://forum.openscad.org/Puzzled-here-tp17172p17185.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

wolf wrote > Sorry, for a moment I did not remember that a module is intended to return > a shape, and a function to return a value. Thus, the confusion. What I > should have sent is this: > > choice = 1; > > echo(myVar()); > Test(myVar()); > > module Test(xyz) > { > if (xyz==1) cube([10,xyz,xyz]); > else if (xyz == 2) cube([xyz,10,xyz]); > else if (xyz == 3) cube([xyz,xyz,10]); > else sphere(10); > } > > function myVar()=choice==1?1: > choice==2?2: > choice==3?3:"not in list"; > > By changing the value assigned to choice, you may change the value > reported by echo() and the shape returned by Test(), which is what -I > think- experimentalist originally intended to do, when we were all mislead > by doug.moen's correct, but misleading answer. > I have read similar questions on these pages before, so the issue is not > new. To overcome it, may I suggest that above sample program be added to > the manual so that others in future can look up the answer there? In > particular, try to stress that module returns a shape, function a value! > > wolf Two comments. 1. In OpenSCAD, modules create shapes but don't return anything. 2. Your function myVar() returns a value which is a function of choice. This is what he original poster wanted. Do not be mislead into thinking it reassigns a value for a variable. Calling it several times while running the script will always return the same value. ----- Larry -- View this message in context: http://forum.openscad.org/Puzzled-here-tp17172p17185.html Sent from the OpenSCAD mailing list archive at Nabble.com.