discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Building an array.

RW
Rogier Wolff
Sat, Sep 9, 2017 10:25 AM

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. 

--
** R.E.Wolff@BitWizard.nl ** 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.

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. -- ** R.E.Wolff@BitWizard.nl ** 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.
NH
nop head
Sat, Sep 9, 2017 10:53 AM

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 R.E.Wolff@bitwizard.nl 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.

--
** R.E.Wolff@BitWizard.nl ** 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
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

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 <R.E.Wolff@bitwizard.nl> 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. > > -- > ** R.E.Wolff@BitWizard.nl ** 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 > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >