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.
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.
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.
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.
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
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.
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.
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
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.