Why does OpenSCAD http://www.openscad.orgversion 2015.03echo "0" for
the code fragment below? Instead of the expected "5".
x=0;
part="test";
if (part=="test") {x=5;}
echo (x);
Paul
Because the x in {x = 5;} is a new x that masks the global one inside the
braces.
if (part=="test") {x=5; echo(x);}
will echo 5 but I expect what you actually want is this:
part="test";
x = (part=="test") ? 5 : 0;
echo (x);
On 31 January 2016 at 19:42, Paul F. Sehorne paul@sehorne.org wrote:
Why does OpenSCAD http://www.openscad.org version 2015.03 echo "0" for
the code fragment below? Instead of the expected "5".
x=0;
part="test";
if (part=="test") {x=5;}
echo (x);
Paul
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
I had to swap
? 5 : 0to read? 0 : 5to make it work.
On Sunday, January 31, 2016 2:54 PM, nop head <nop.head@gmail.com> wrote:
Because the x in {x = 5;} is a new x that masks the global one inside the braces.
if (part=="test") {x=5; echo(x);}
will echo 5 but I expect what you actually want is this:
part="test";
x = (part=="test") ? 5 : 0;
echo (x);
On 31 January 2016 at 19:42, Paul F. Sehorne paul@sehorne.org wrote:
Why does OpenSCAD version 2015.03 echo "0" for the code fragment below? Instead of the expected "5".
x=0;
part="test";
if (part=="test") {x=5;}
echo (x);
Paul
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
If you do that it will print 0 instead of 5. The OP wanted 5.
On 31 January 2016 at 19:58, fred fred_dot_u@yahoo.com wrote:
I had to swap
? 5 : 0
to read
? 0 : 5
to make it work.
On Sunday, January 31, 2016 2:54 PM, nop head nop.head@gmail.com wrote:
Because the x in {x = 5;} is a new x that masks the global one inside the
braces.
if (part=="test") {x=5; echo(x);}
will echo 5 but I expect what you actually want is this:
part="test";
x = (part=="test") ? 5 : 0;
echo (x);
On 31 January 2016 at 19:42, Paul F. Sehorne paul@sehorne.org wrote:
Why does OpenSCAD version 2015.03 echo "0" for the code fragment below?
Instead of the expected "5".
x=0;
part="test";
if (part=="test") {x=5;}
echo (x);
Paul
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
because OpenSCAD isn't a sequential language, it's functional.
Once a variable is declared, it remains fixed at the provided value until
it goes out of scope.
You create a new x inside the braces, set it to 5, then immediately
discard it. Your echo statement spits out the value of the toplevel x,
which you've set to zero.
OpenSCAD is not like C/Java/Python/whatever else you're used to. It's got
its own thing going on which, once you get used to it, has its own beauty.