discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Incrementing a variable doesn't work as expected.

N
nigeljohnson73
Wed, Apr 1, 2015 9:26 AM

function cpos(s, n) = (n == -1) ? (0) : ( cpos(s,n-1) + (s[n]==".") ? 2 : 4
);

// run through expected results from cpos ("--.-", 3):

s = "--.-";

echo (s[0]=="."? 2 : 4); // Outputs "ECHO: 4" - CORRECT
echo (s[1]=="."? 2 : 4); // Outputs "ECHO: 4" - CORRECT
echo (s[2]=="."? 2 : 4); // Outputs "ECHO: 2" - CORRECT
echo (s[3]=="."? 2 : 4); // Outputs "ECHO: 4" - CORRECT

echo (  0                // n = -1
+ s[0]=="."?2:4  // n = 0 so should be 4
+ s[1]=="."?2:4  // n = 1 so should be 4
+ s[2]=="."?2:4  // n = 2 so should be 2
+ s[3]=="."?2:4  // n = 3 so should be 4
); // Outputs "ECHO: 4" - should be 0 + 4 + 4 + 2 + 4 = 14

--
View this message in context: http://forum.openscad.org/Incrementing-a-variable-doesn-t-work-as-expected-tp12271p12281.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

function cpos(s, n) = (n == -1) ? (0) : ( cpos(s,n-1) + (s[n]==".") ? 2 : 4 ); // run through expected results from cpos ("--.-", 3): s = "--.-"; echo (s[0]=="."? 2 : 4); // Outputs "ECHO: 4" - CORRECT echo (s[1]=="."? 2 : 4); // Outputs "ECHO: 4" - CORRECT echo (s[2]=="."? 2 : 4); // Outputs "ECHO: 2" - CORRECT echo (s[3]=="."? 2 : 4); // Outputs "ECHO: 4" - CORRECT echo ( 0 // n = -1 + s[0]=="."?2:4 // n = 0 so should be 4 + s[1]=="."?2:4 // n = 1 so should be 4 + s[2]=="."?2:4 // n = 2 so should be 2 + s[3]=="."?2:4 // n = 3 so should be 4 ); // Outputs "ECHO: 4" - should be 0 + 4 + 4 + 2 + 4 = 14 -- View this message in context: http://forum.openscad.org/Incrementing-a-variable-doesn-t-work-as-expected-tp12271p12281.html Sent from the OpenSCAD mailing list archive at Nabble.com.
M
MichaelAtOz
Wed, Apr 1, 2015 9:57 AM

BTW you may want to check out  assign() [depricated]
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Conditional_and_Iterator_Functions#Assign_Statement
and  this.
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/List_Comprehensions


Unless specifically shown otherwise above, my contribution is in the Public Domain; To the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. This work is published globally via the internet. :) Inclusion of works of previous authors is not included in the above.

The TPP is no simple “trade agreement.”  Fight it! http://www.ourfairdeal.org/

View this message in context: http://forum.openscad.org/Incrementing-a-variable-doesn-t-work-as-expected-tp12271p12282.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

BTW you may want to check out assign() [depricated] <https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Conditional_and_Iterator_Functions#Assign_Statement> and this. <https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/List_Comprehensions> ----- Unless specifically shown otherwise above, my contribution is in the Public Domain; To the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. This work is published globally via the internet. :) Inclusion of works of previous authors is not included in the above. The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ -- View this message in context: http://forum.openscad.org/Incrementing-a-variable-doesn-t-work-as-expected-tp12271p12282.html Sent from the OpenSCAD mailing list archive at Nabble.com.
NH
nop head
Wed, Apr 1, 2015 10:07 AM

Your last example works if you add some brackets

echo (  0                // n = -1
+ (s[0]=="."?2:4)  // n = 0 so should be 4
+ (s[1]=="."?2:4)  // n = 1 so should be 4
+ (s[2]=="."?2:4)  // n = 2 so should be 2
+ (s[3]=="."?2:4)  // n = 3 so should be 4
); // Outputs "ECHO: 14"

On 1 April 2015 at 10:57, MichaelAtOz oz.at.michael@gmail.com wrote:

and  this.
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/List_Comprehensions


Unless specifically shown otherwise above, my contribution is in the
Public Domain; To the extent possible under law, I have waived all
copyright and related or neighbouring rights to this work. This work is
published globally via the internet. :) Inclusion of works of previous
authors is not included in the above.

The TPP is no simple "trade agreement."  Fight it!
http://www.ourfairdeal.org/

View this message in context:
http://forum.openscad.org/Incrementing-a-variable-doesn-t-work-as-expected-tp12271p12282.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

Your last example works if you add some brackets echo ( 0 // n = -1 + (s[0]=="."?2:4) // n = 0 so should be 4 + (s[1]=="."?2:4) // n = 1 so should be 4 + (s[2]=="."?2:4) // n = 2 so should be 2 + (s[3]=="."?2:4) // n = 3 so should be 4 ); // Outputs "ECHO: 14" On 1 April 2015 at 10:57, MichaelAtOz <oz.at.michael@gmail.com> wrote: > BTW you may want to check out assign() [depricated] > < > https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Conditional_and_Iterator_Functions#Assign_Statement > > > and this. > <https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/List_Comprehensions> > > > > ----- > Unless specifically shown otherwise above, my contribution is in the > Public Domain; To the extent possible under law, I have waived all > copyright and related or neighbouring rights to this work. This work is > published globally via the internet. :) Inclusion of works of previous > authors is not included in the above. > > The TPP is no simple "trade agreement." Fight it! > http://www.ourfairdeal.org/ > -- > View this message in context: > http://forum.openscad.org/Incrementing-a-variable-doesn-t-work-as-expected-tp12271p12282.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 >
N
nigeljohnson73
Wed, Apr 1, 2015 10:10 AM

And there's the answer - brackets :)

function cpos(s, n) = (n == 0) ? (0) : ( cpos(s,n-1) + ((s[n-1]==".") ? 2 :
4 ));

Now works as expected.

Thanks guys!!!
N.

--
View this message in context: http://forum.openscad.org/Incrementing-a-variable-doesn-t-work-as-expected-tp12271p12284.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

And there's the answer - brackets :) function cpos(s, n) = (n == 0) ? (0) : ( cpos(s,n-1) + ((s[n-1]==".") ? 2 : 4 )); Now works as expected. Thanks guys!!! N. -- View this message in context: http://forum.openscad.org/Incrementing-a-variable-doesn-t-work-as-expected-tp12271p12284.html Sent from the OpenSCAD mailing list archive at Nabble.com.
NH
nop head
Wed, Apr 1, 2015 10:10 AM

You need the same brackets in cpos() otherwise it does the add before the
?. You also have lots of brackets where you don't need them.

function cpos(s, n) = n == -1 ? 0 : cpos(s,n-1) + (s[n]=="." ? 2 : 4 );

On 1 April 2015 at 11:07, nop head nop.head@gmail.com wrote:

Your last example works if you add some brackets

echo (  0                // n = -1
+ (s[0]=="."?2:4)  // n = 0 so should be 4
+ (s[1]=="."?2:4)  // n = 1 so should be 4
+ (s[2]=="."?2:4)  // n = 2 so should be 2
+ (s[3]=="."?2:4)  // n = 3 so should be 4
); // Outputs "ECHO: 14"

On 1 April 2015 at 10:57, MichaelAtOz oz.at.michael@gmail.com wrote:

and  this.
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/List_Comprehensions


Unless specifically shown otherwise above, my contribution is in the
Public Domain; To the extent possible under law, I have waived all
copyright and related or neighbouring rights to this work. This work is
published globally via the internet. :) Inclusion of works of previous
authors is not included in the above.

The TPP is no simple "trade agreement."  Fight it!
http://www.ourfairdeal.org/

View this message in context:
http://forum.openscad.org/Incrementing-a-variable-doesn-t-work-as-expected-tp12271p12282.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

You need the same brackets in cpos() otherwise it does the add before the ?. You also have lots of brackets where you don't need them. function cpos(s, n) = n == -1 ? 0 : cpos(s,n-1) + (s[n]=="." ? 2 : 4 ); On 1 April 2015 at 11:07, nop head <nop.head@gmail.com> wrote: > Your last example works if you add some brackets > > echo ( 0 // n = -1 > + (s[0]=="."?2:4) // n = 0 so should be 4 > + (s[1]=="."?2:4) // n = 1 so should be 4 > + (s[2]=="."?2:4) // n = 2 so should be 2 > + (s[3]=="."?2:4) // n = 3 so should be 4 > ); // Outputs "ECHO: 14" > > > On 1 April 2015 at 10:57, MichaelAtOz <oz.at.michael@gmail.com> wrote: > >> BTW you may want to check out assign() [depricated] >> < >> https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Conditional_and_Iterator_Functions#Assign_Statement >> > >> and this. >> <https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/List_Comprehensions> >> >> >> >> ----- >> Unless specifically shown otherwise above, my contribution is in the >> Public Domain; To the extent possible under law, I have waived all >> copyright and related or neighbouring rights to this work. This work is >> published globally via the internet. :) Inclusion of works of previous >> authors is not included in the above. >> >> The TPP is no simple "trade agreement." Fight it! >> http://www.ourfairdeal.org/ >> -- >> View this message in context: >> http://forum.openscad.org/Incrementing-a-variable-doesn-t-work-as-expected-tp12271p12282.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 >> > >
N
nigeljohnson73
Wed, Apr 1, 2015 10:22 AM

I'm usually an avid bracketer for readability and avoidance of doubt. This
one beat me

Thanks again.

--
View this message in context: http://forum.openscad.org/Incrementing-a-variable-doesn-t-work-as-expected-tp12271p12286.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

I'm usually an avid bracketer for readability and avoidance of doubt. This one beat me Thanks again. -- View this message in context: http://forum.openscad.org/Incrementing-a-variable-doesn-t-work-as-expected-tp12271p12286.html Sent from the OpenSCAD mailing list archive at Nabble.com.