# Incrementing a variable doesn't work as expected.

16 messages
Open this post in threaded view
|

## Incrementing a variable doesn't work as expected.

 I'm trying to do some morse code, but I'm struggling to work out the start position of the 'dit' and the 'dah'... I think first, the code... id=1;wt=1; // for a ring, don't worry about these lu = 1; // letter unit. a dit is 1 unit wide, a dah is 3, and the gap between them is 1. module drawChar(s) {         echo("char: ", s);         pos = 0;         for(i = [0:len(s)-1]) {                 inc = 0;                 if(s[i] == ".") {                         echo(s[i], "dit ",pos," ", inc);                         translate([pos+lu/2,0,0]) cylinder(r=lu/2, h = (id+wt+2)/2);                         inc = (1 + 1)*lu;                 } else if(s[i] == "-") {                         echo(s[i], "dah ",pos," ", inc);                         translate([pos,-lu/2,0]) cube([3*lu,lu,(id+wt+2)/2]);                         inc = (3+1)*lu;                 }                 pos = pos + inc; // the inc seems to have gone out of scope         } } drawChar("--.-"); // this is a 'q' This is the output: ECHO: "char: ", "--.-" ECHO: "-", "dah ", 0, " ", 4 ECHO: "-", "dah ", 0, " ", 4 ECHO: ".", "dit ", 0, " ", 2 ECHO: "-", "dah ", 0, " ", 4 I have tried many ways to calculate 'pos' - the starting point of the dit/dah. So the bit that's getting me is the way assignments seem to work - basically pos always stays zero. Also at the point of echoing inc, I'm expecting it to still be zero. 2 questions I suppose, a learning point for me, what's going on? and second, how do I make it do what I want it to do? I was thinking of some way to count the occurances of '.' and '-' in the array as a function multiplying accordingly, but my brain melted a little - it is early. Any help much appreciated Nigel
Open this post in threaded view
|

## Re: Incrementing a variable doesn't work as expected.

 For info, here is my recursive position counter that fails: function cpos(s, n) = cpos(s,n-1) + (n == -1) ? (0) : ( (s[n]==".") ? 2 : 4); when I use it instead of pos in the x coordinate for example in the dit: translate([cpos(s,i)+lu/2,0,0]) cylinder(r=lu/2, h = (id+wt+2)/2); it says recursion detected. It's not lying  but the manual says I can do recursion with the trigraph stuff.
Open this post in threaded view
|

## Re: Incrementing a variable doesn't work as expected.

 You can't modify variables, so not only is inc out of scope but you can't change the value of pos.Recursion is the right way to do it but you have infinite recursion because you unconditionally call cpos(s,n-1).On 1 April 2015 at 08:44, nigeljohnson73 wrote:For info, here is my recursive position counter that fails: function cpos(s, n) = cpos(s,n-1) + (n == -1) ? (0) : ( (s[n]==".") ? 2 : 4); when I use it instead of pos in the x coordinate for example in the dit: translate([cpos(s,i)+lu/2,0,0]) cylinder(r=lu/2, h = (id+wt+2)/2); it says recursion detected. It's not lying  but the manual says I can do recursion with the trigraph stuff. -- View this message in context: http://forum.openscad.org/Incrementing-a-variable-doesn-t-work-as-expected-tp12271p12272.html Sent from the OpenSCAD mailing list archive at Nabble.com. _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: Incrementing a variable doesn't work as expected.

Open this post in threaded view
|

## Re: Incrementing a variable doesn't work as expected.

 This post was updated on . In reply to this post by nophead Doh!!! yep. I moved the conditional calling, but this is still not playing nicely: function cpos(s, n) = (n == -1) ? (0) : ( cpos(s,n-1) + (s[n]==".") ? 2 : 4 ); echo ("--.-, 0: ", cpos("--.-",0)); echo ("--.-, 1: ", cpos("--.-",1)); echo ("--.-, 2: ", cpos("--.-",2)); echo ("--.-, 3: ", cpos("--.-",3)); ECHO: "--.-, 0: ", 4 ECHO: "--.-, 1: ", 4 ECHO: "--.-, 2: ", 4 ECHO: "--.-, 3: ", 4 I have also noticed a problem in that 0 should be 0 which I'll fix later, but that aside, I would expect the following ECHO: "--.-, 0: ", 4 ECHO: "--.-, 1: ", 8 ECHO: "--.-, 2: ", 10 ECHO: "--.-, 3: ", 14
Open this post in threaded view
|

## Re: Incrementing a variable doesn't work as expected.

 In reply to this post by Peter Falke Sorry, yep, I'm using the latest release 2015.03 on both mac and windows
Open this post in threaded view
|

## Re: Incrementing a variable doesn't work as expected.

Open this post in threaded view
|

## Re: Incrementing a variable doesn't work as expected.

 Yep, I'm now using recursion to bypass that :) but it seems I'm a bit rusty at that as well.
Open this post in threaded view
|

## Re: Incrementing a variable doesn't work as expected.

Open this post in threaded view
|

## Re: Incrementing a variable doesn't work as expected.

 Yeah I'm not doing that any more because it doesn't work. I've moved to recursion. Possibly an email lag? This is what is now not working: function cpos(s, n) = (n == -1) ? (0) : ( cpos(s,n-1) + (s[n]==".") ? 2 : 4 ); cpos("--.-",3) returns 4, instead of 14.
Open this post in threaded view
|

## Re: Incrementing a variable doesn't work as expected.

 In reply to this post by nigeljohnson73 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
Open this post in threaded view
|

## Re: Incrementing a variable doesn't work as expected.

 Administrator BTW you may want to check out assign() [depricated] and this. Admin - email* me if you need anything, or if I've done something stupid... * click on my MichaelAtOz label, there is a link to email me. 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. Obviously 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/ time is running out!
Open this post in threaded view
|

## Re: Incrementing a variable doesn't work as expected.

Open this post in threaded view
|

## Re: Incrementing a variable doesn't work as expected.

 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.