[OpenSCAD] for loop in a function

Frank van der Hulst drifter.frank at gmail.com
Tue Apr 10 20:17:22 EDT 2018


>From the manual:

 // recursion - find the sum of the values in a vector (array) by calling itself
 // from the start (or s'th element) to the i'th element - remember
elements are zero based

 function sumv(v,i,s=0) = (i==s ? v[i] : v[i] + sumv(v,i-1,s));

 vec=[ 10, 20, 30, 40 ];
 echo("sum vec=", sumv(vec,2,1)); // calculates 20+30=50




On Wed, Apr 11, 2018 at 11:34 AM, Mint86 <eric23b at gmail.com> wrote:

> Hi
>
> I haven't been able to get a for loop working in a function. I'm trying to
> add the values in a vector from a start point to an end point.
>
> Something like this:
>
> AVector = [2,5,3,11,4];
> start = 1;
> end = 3;
>
> //addVector would return 19
>
> function addVector (aVector,start,end)
>
> Any help would be great. Thanks!
>
>
>
> --
> Sent from: http://forum.openscad.org/
>
> _______________________________________________
> OpenSCAD mailing list
> Discuss at lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscad.org/pipermail/discuss_lists.openscad.org/attachments/20180411/50b9f401/attachment.html>


More information about the Discuss mailing list