Incrementing a variable doesn't work as expected.

classic Classic list List threaded Threaded
16 messages Options
Reply | Threaded
Open this post in threaded view
|

Incrementing a variable doesn't work as expected.

nigeljohnson73
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
Reply | Threaded
Open this post in threaded view
|

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

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

Reply | Threaded
Open this post in threaded view
|

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

nophead
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 <[hidden email]> 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
Reply | Threaded
Open this post in threaded view
|

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

Peter Falke
In reply to this post by nigeljohnson73
Are ypu using a current version of OpenSCAD? The latest?


2015-04-01 9:44 GMT+02:00 nigeljohnson73 <[hidden email]>:
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



--
[hidden email]

P.S. Falls meine E-Mail kürzer ausfällt als Dir angenehm ist:
Ich probiere gerade aus kurze Antworten statt gar keine Antworten zu schreiben.
Wenn Du gerne mehr lesen möchtest, dann lass es mich bitte wissen.

P.S. In case my e-mail is shorter than you enjoy:
I am currently trying short replies instead of no replies at all.
Please let me know, if you like to read more.

Enjoy!

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

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

nigeljohnson73
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
Reply | Threaded
Open this post in threaded view
|

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

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

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

Peter Falke
In reply to this post by Peter Falke
You can not reassign variables. Variables in OpenSCAD are constants.
Please have a look at the manual:
http://en.wikibooks.org/wiki/OpenSCAD_User_Manual/The_OpenSCAD_Language#Variables

2015-04-01 10:50 GMT+02:00 Peter Falke <[hidden email]>:
Are ypu using a current version of OpenSCAD? The latest?


2015-04-01 9:44 GMT+02:00 nigeljohnson73 <[hidden email]>:
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



--
[hidden email]

P.S. Falls meine E-Mail kürzer ausfällt als Dir angenehm ist:
Ich probiere gerade aus kurze Antworten statt gar keine Antworten zu schreiben.
Wenn Du gerne mehr lesen möchtest, dann lass es mich bitte wissen.

P.S. In case my e-mail is shorter than you enjoy:
I am currently trying short replies instead of no replies at all.
Please let me know, if you like to read more.

Enjoy!



--
[hidden email]

P.S. Falls meine E-Mail kürzer ausfällt als Dir angenehm ist:
Ich probiere gerade aus kurze Antworten statt gar keine Antworten zu schreiben.
Wenn Du gerne mehr lesen möchtest, dann lass es mich bitte wissen.

P.S. In case my e-mail is shorter than you enjoy:
I am currently trying short replies instead of no replies at all.
Please let me know, if you like to read more.

Enjoy!

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

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

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

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

Peter Falke
In reply to this post by Peter Falke
              pos = pos + inc; // the inc seems to have gone out of scope

This is the problem: this is the last definition for pos, so only this one is used (pos=0 will be ignored).
And it is undefined, as when the left side is assigned the right side has no meaning,yet. Its a recursion.

2015-04-01 10:54 GMT+02:00 Peter Falke <[hidden email]>:
You can not reassign variables. Variables in OpenSCAD are constants.
Please have a look at the manual:
http://en.wikibooks.org/wiki/OpenSCAD_User_Manual/The_OpenSCAD_Language#Variables

2015-04-01 10:50 GMT+02:00 Peter Falke <[hidden email]>:
Are ypu using a current version of OpenSCAD? The latest?


2015-04-01 9:44 GMT+02:00 nigeljohnson73 <[hidden email]>:
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



--
[hidden email]

P.S. Falls meine E-Mail kürzer ausfällt als Dir angenehm ist:
Ich probiere gerade aus kurze Antworten statt gar keine Antworten zu schreiben.
Wenn Du gerne mehr lesen möchtest, dann lass es mich bitte wissen.

P.S. In case my e-mail is shorter than you enjoy:
I am currently trying short replies instead of no replies at all.
Please let me know, if you like to read more.

Enjoy!



--
[hidden email]

P.S. Falls meine E-Mail kürzer ausfällt als Dir angenehm ist:
Ich probiere gerade aus kurze Antworten statt gar keine Antworten zu schreiben.
Wenn Du gerne mehr lesen möchtest, dann lass es mich bitte wissen.

P.S. In case my e-mail is shorter than you enjoy:
I am currently trying short replies instead of no replies at all.
Please let me know, if you like to read more.

Enjoy!



--
[hidden email]

P.S. Falls meine E-Mail kürzer ausfällt als Dir angenehm ist:
Ich probiere gerade aus kurze Antworten statt gar keine Antworten zu schreiben.
Wenn Du gerne mehr lesen möchtest, dann lass es mich bitte wissen.

P.S. In case my e-mail is shorter than you enjoy:
I am currently trying short replies instead of no replies at all.
Please let me know, if you like to read more.

Enjoy!

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

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

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

Reply | Threaded
Open this post in threaded view
|

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

nigeljohnson73
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
Reply | Threaded
Open this post in threaded view
|

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

MichaelAtOz
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!
Reply | Threaded
Open this post in threaded view
|

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

nophead
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 <[hidden email]> 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
[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
Reply | Threaded
Open this post in threaded view
|

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

nigeljohnson73
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.
Reply | Threaded
Open this post in threaded view
|

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

nophead
In reply to this post by nophead
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 <[hidden email]> 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 <[hidden email]> 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
[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
Reply | Threaded
Open this post in threaded view
|

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

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

Thanks again.