[OpenSCAD] self modify array

Ronaldo rcmpersiano at gmail.com
Sun Oct 16 15:37:13 EDT 2016


The following line in your code:

 let(myArray = array_replace_at(2,myArray,myReplacementArray)) 

will not do what you expect because all variables in OpenSCAD are a
immutable and cannot be changed. Instead of changes you need to generate a
new array based on the previous one. If the new array generation depends not
only on the input array but on part of itself, the only way I know to do it
is by recursion. 

Here is an example. Given one input array, I want the array of the
accumulated sums of it, that is, in almost plain english:

   output[i] = sum(input[0:i]) for all i 

To generate output from input consider the following recursive code:

> function accumulated_sums(input, i=0, acc_sum=[]) =
>     i == len(input) ?
>         acc_sum :
>         let( sum_i = (i==0 ? input[0]: acc_sum[i-1] + input[i]) )
>         accumulated_sums(input, i=i+1, acc_sum = concat(acc_sum, [ sum_i
> ]) );
> 
> echo(accumulated_sums([0,1,2,3,4]));
> // ECHO: [0, 1, 3, 6, 10]





--
View this message in context: http://forum.openscad.org/self-modify-array-tp18718p18740.html
Sent from the OpenSCAD mailing list archive at Nabble.com.




More information about the Discuss mailing list