Building an array.

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

Building an array.

rew
Hi,

I'm building a model that requires an "arbitrary function".

So I have

values= [0,1,4,9,16,25];

Now the simple example here, those could easily be calculated from the
index, but in my actual situation that is not the case.

Now I would like to build an array of points
 [[0,0], [1,1], [2,4], [3,9], [4,16], [5,25]];

In a normal imperative language i'd just do
v = [];
for (i in values)
  v = concat (v, [i, values[i] ]);

(In openscad I think I'm needing an extra set of brackets, right?)

Now I could do the legwork and write out the whole array, but I also
need a bunch of derived versions. for example:
 w = concat (w, [i, a*values[i]+b ]);

In openscad I'm kind of out-of-luck, right?

        Roger.

--
** [hidden email] ** http://www.BitWizard.nl/ ** +31-15-2600998 **
**    Delftechpark 26 2628 XH  Delft, The Netherlands. KVK: 27239233    **
*-- BitWizard writes Linux device drivers for any device you may have! --*
The plan was simple, like my brother-in-law Phil. But unlike
Phil, this plan just might work.

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

Re: Building an array.

nophead
You can't modify variables but you can write a recursive function or a list comprehension to build arrays.

So:

values= [0,1,4,9,16,25];
v= [for(i = [0 : len(values) - 1]) [i, values[i]] ];
echo(v);

ECHO: [[0, 0], [1, 1], [2, 4], [3, 9], [4, 16], [5, 25]]


On 9 September 2017 at 11:25, Rogier Wolff <[hidden email]> wrote:
Hi,

I'm building a model that requires an "arbitrary function".

So I have

values= [0,1,4,9,16,25];

Now the simple example here, those could easily be calculated from the
index, but in my actual situation that is not the case.

Now I would like to build an array of points
 [[0,0], [1,1], [2,4], [3,9], [4,16], [5,25]];

In a normal imperative language i'd just do
v = [];
for (i in values)
  v = concat (v, [i, values[i] ]);

(In openscad I think I'm needing an extra set of brackets, right?)

Now I could do the legwork and write out the whole array, but I also
need a bunch of derived versions. for example:
 w = concat (w, [i, a*values[i]+b ]);

In openscad I'm kind of out-of-luck, right?

        Roger.

--
** [hidden email] ** http://www.BitWizard.nl/ ** <a href="tel:%2B31-15-2600998" value="+31152600998">+31-15-2600998 **
**    Delftechpark 26 2628 XH  Delft, The Netherlands. KVK: 27239233    **
*-- BitWizard writes Linux device drivers for any device you may have! --*
The plan was simple, like my brother-in-law Phil. But unlike
Phil, this plan just might work.

_______________________________________________
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