# incrementing a variable and other confusions for a procedural coder

13 messages
Open this post in threaded view
|

## incrementing a variable and other confusions for a procedural coder

 I’ve been using OpenSCAD for about two years and I just realized that I don’t know how to something simple like increment a variable or pass a value from one part of my code to another. Case in point.  I have a vector of vectors: x = [[3,5],[1,6],[6,1],[8,4]]  I’d like to count the number of times that the first element of each sub vector exceeds 2. I think I can iterate through x with a for loop and ask whether x[i][0] > 1, but I don’t know what to do with the result of that comparison. k=0 then k=k+1 isn’t supported in the language.  And even if it was, k is undefined outside of the loop. A function may be helpful, but it looks like a function is just a line of code that has been set aside for modularity.  Search can iterate across a vector but any comparison has to be equality, nothing as fancy as “greater than”. Ken_______________________________________________ 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 and other confusions for a procedural coder

 A recursive function  can do what you want.function count_more_than(v, n = 2, i = 0, count = 0) = i >= len(v) ? count : count_more_than(v, n, i + 1, count = v[i][0] > n ? count + 1 : count);x = [[3,5],[1,6],[6,1],[8,4]];echo(count_more_than(x));On Fri, 27 Nov 2020 at 22:54, <[hidden email]> wrote:I’ve been using OpenSCAD for about two years and I just realized that I don’t know how to something simple like increment a variable or pass a value from one part of my code to another. Case in point.  I have a vector of vectors: x = [[3,5],[1,6],[6,1],[8,4]]  I’d like to count the number of times that the first element of each sub vector exceeds 2. I think I can iterate through x with a for loop and ask whether x[i][0] > 1, but I don’t know what to do with the result of that comparison. k=0 then k=k+1 isn’t supported in the language.  And even if it was, k is undefined outside of the loop. A function may be helpful, but it looks like a function is just a line of code that has been set aside for modularity.  Search can iterate across a vector but any comparison has to be equality, nothing as fancy as “greater than”. Ken_______________________________________________ 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 and other confusions for a procedural coder

Open this post in threaded view
|

## Re: incrementing a variable and other confusions for a procedural coder

 CONTENTS DELETED The author has deleted this message.
Open this post in threaded view
|

## Re: incrementing a variable and other confusions for a procedural coder

 Yes, C and twenty or thirty other languages. Plus a few domain specific languages I wrote.  -RevarOn Nov 27, 2020, at 5:01 PM, David <[hidden email]> wrote:﻿ Revar, you must be a C programmer! On 11/27/20 5:39 PM, Revar Desmera wrote: Don’t even need to go recursive for this.  len([for (x=v) if(x[0]>n) 1]) On Nov 27, 2020, at 3:05 PM, nop head [hidden email] wrote: ﻿ A recursive function  can do what you want. function count_more_than(v, n = 2, i = 0, count = 0) = i >= len(v) ? count : count_more_than(v, n, i + 1, count = v[i][0] > n ? count + 1 : count); x = [[3,5],[1,6],[6,1],[8,4]]; echo(count_more_than(x)); On Fri, 27 Nov 2020 at 22:54, <[hidden email]> wrote: I’ve been using OpenSCAD for about two years and I just realized that I don’t know how to something simple like increment a variable or pass a value from one part of my code to another.   Case in point.  I have a vector of vectors: x = [[3,5],[1,6],[6,1],[8,4]]  I’d like to count the number of times that the first element of each sub vector exceeds 2.   I think I can iterate through x with a for loop and ask whether x[i][0] > 1, but I don’t know what to do with the result of that comparison. k=0 then k=k+1 isn’t supported in the language.  And even if it was, k is undefined outside of the loop.   A function may be helpful, but it looks like a function is just a line of code that has been set aside for modularity.  Search can iterate across a vector but any comparison has to be equality, nothing as fancy as “greater than”.   Ken _______________________________________________ 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 ```_______________________________________________ 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_______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## What am I doing wrong?

 In reply to this post by skypuppy CONTENTS DELETED The author has deleted this message.
Open this post in threaded view
|

## Re: What am I doing wrong?

 CONTENTS DELETED The author has deleted this message.
Open this post in threaded view
|

## Re: What am I doing wrong?

 In reply to this post by skypuppy Hard to help without seeing your code. On 4 Dec 2020 at 23:52, David wrote: > Trying to make a mount for a 3D printer sensor.  Constructing all the > basic stuff from large basic blocks. > > What's wrong with there I am?  The subtraction is one doing what I would > like it to do.  Something having to do with the faces is my guess but > actually, I'm clueless.  :( > > The last construction I thought would be a cutaway, leaving the vertical > arm (I think) of the BL Touch mount for a Creality CR-10 S5 printer.  I > could include the STL if the non-parameterized version of need be > if/when I can find one, probably on thingiverse. > > This is from scratch with nothing streamlined and no efficiency attempts > in the code yet.  Just straight hunt-and-peck so far. _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: What am I doing wrong?

 CONTENTS DELETED The author has deleted this message.
Open this post in threaded view
|

## Re: What am I doing wrong?

 Well, it's definitely not the way I would go about it, so here's what I would do. You'll have to play with the values, and you are certainly welcome to put them into variables. Note the pound sign (#). It will help you see where the line puts the object. difference() {         union() {                 translate([0,18,0])                         cube([37,16.67,4]);                 translate([22,18,0])                         cube([6,16.67,30]);                 translate([28,0,0])                         cube([9,19,4]);         }         translate([13,22,-.001]) # cylinder(h=6,d=3);         translate([31,4,-.001]) # cylinder(h=6,d=3); }   -- Sent from: http://forum.openscad.org/_______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: What am I doing wrong?

 CONTENTS DELETED The author has deleted this message.