[OpenSCAD] Incrementing a variable doesn't work as expected.

nigeljohnson73 nigel at nigeljohnson.net
Wed Apr 1 05:26:49 EDT 2015


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.




More information about the Discuss mailing list