# double for()

14 messages
Open this post in threaded view
|

## double for()

 Hello again. It's the first time I use declarative language, so there are some thing which I don't know how to do... I would like to do something like that in Openscad : n=0; it = [4,2,0,1,5]; for(i = [0 : 5]) {         for(j = [0 : it[i]) { // (it[i] is not always the same value)                 n = n+j; // This line is my problem : I can't do this :/                 echo(i, j, n); // I want to use 'n'                 ....         } } I tried with assign, but I have not succeeded. Maybe I could do this with a concat() function, containing the vector of vector to a big vector, but it's not implemented yetThanks, Roipou
Open this post in threaded view
|

## Re: double for()

 You have to write a recursive function to do that rather than iteration.On 12 November 2013 19:30, roipoussiere wrote: Hello again. It's the first time I use declarative language, so there are some thing which I don't know how to do... I would like to do something like that in Openscad : n=0; it = [4,2,0,1,5]; for(i = [0 : 5]) {         for(j = [0 : it[i]) { // (it[i] is not always the same value)                 n = n+j; // This line is my problem : I can't do this :/                 echo(i, j, n); // I want to use 'n'                 ....         } } I tried with assign, but I have not succeeded. Maybe I could do this with a concat() function, containing the vector of vector to a big vector, but  it's not implemented yet Thanks, Roipou -- View this message in context: http://forum.openscad.org/double-for-tp5932.html Sent from the OpenSCAD mailing list archive at Nabble.com. _______________________________________________ OpenSCAD mailing list [hidden email] http://rocklinux.net/mailman/listinfo/openscad http://openscad.org - https://flattr.com/thing/121566 _______________________________________________ OpenSCAD mailing list [hidden email] http://rocklinux.net/mailman/listinfo/openscadhttp://openscad.org - https://flattr.com/thing/121566
Open this post in threaded view
|

## Re: double for()

Open this post in threaded view
|

## Re: double for()

 Administrator In reply to this post by roipoussiere two variable recursion or if the vector is small just pre-calculate a 2D vector (matrix) and use i,j to retrieve the value. Admin - email* me if you need anything, or if I've done something stupid... * click on my MichaelAtOz label, there is a link to email me. Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above. The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out!
Open this post in threaded view
|

## Re: double for()

Open this post in threaded view
|

## Re: double for()

Open this post in threaded view
|

## Re: double for()

 Whosawhatsis wrote I would definitely find that useful. On Tuesday, November 12, 2013 at 2:21 PM, Ruud Vlaming wrote: > In my hack of openscad (2013.06 + hacks) > https://github.com/devlaam/openscad> you can do things like: > > function p(t) = for(i=[0:t],[i]); > function q(t) = for(i=[0:t],[i,2*i,t]); > The functionality looks quite useful. The syntax does break any expectations though. I guess it should be at least renamed. -- Torsten
Open this post in threaded view
|

## Re: double for()

 This is how to do it without changing the language:n=0;it = [4,2,0,1,5]; // sum of arithmentic progressionfunction ap(first, last) = (last - first + 1) * (first + last) / 2;// sum of a list of aritmetic progressions function sumap(i) = i >= 0 ? ap(0, it[i]) + sumap(i - 1) : 0;  for(i = [0 : len(it) - 1]) {        for(j = [0 : it[i]]) // (it[i] is not always the same value)             assign(n = sumap(i - 1) + ap(0, j)) {                echo(i, j, n); // I want to use 'n'        }} It gives the following:ECHO: 0, 0, 0ECHO: 0, 1, 1ECHO: 0, 2, 3ECHO: 0, 3, 6ECHO: 0, 4, 10ECHO: 1, 0, 10ECHO: 1, 1, 11 ECHO: 1, 2, 13ECHO: 2, 0, 13ECHO: 3, 0, 13ECHO: 3, 1, 14ECHO: 4, 0, 14ECHO: 4, 1, 15ECHO: 4, 2, 17ECHO: 4, 3, 20ECHO: 4, 4, 24 ECHO: 4, 5, 29On 12 November 2013 22:51, tp3 wrote: Whosawhatsis wrote > I would definitely find that useful. > > On Tuesday, November 12, 2013 at 2:21 PM, Ruud Vlaming wrote: > >> In my hack of openscad (2013.06 + hacks) >> https://github.com/devlaam/openscad >> you can do things like: >> >> function p(t) = for(i=[0:t],[i]); >> function q(t) = for(i=[0:t],[i,2*i,t]); >> The functionality looks quite useful. The syntax does break any expectations though. I guess it should be at least renamed. -- View this message in context: http://forum.openscad.org/double-for-tp5932p5938.html Sent from the OpenSCAD mailing list archive at Nabble.com. _______________________________________________ OpenSCAD mailing list [hidden email] http://rocklinux.net/mailman/listinfo/openscad http://openscad.org - https://flattr.com/thing/121566 _______________________________________________ OpenSCAD mailing list [hidden email] http://rocklinux.net/mailman/listinfo/openscadhttp://openscad.org - https://flattr.com/thing/121566
Open this post in threaded view
|

## Re: double for()

Open this post in threaded view
|

## Re: double for()

 Thanks a lot for all your answers ! You are very nice !! So : In my hack of openscad (2013.06 + hacks)     https://github.com/devlaam/openscadyou can do things like:   function p(t) = for(i=[0:t],[i]);   function q(t) = for(i=[0:t],[i,2*i,t]); [...]I find this function very very interesting and usefull ! However I would like to publish my creation with Customizer from Thingiverse, which run on the official OpenScad version. So I can't use your hack, but I hope your work will be implemented in next versions of OpenScad. This is how to do it without changing the language: [...]Thanks !! I also thought than recursive function was the right way ! However it's not exactly what I want, because 'n' must be the global index, not a sum. I would like something like this : ECHO: 0, 0, 0 ECHO: 0, 1, 1 ECHO: 0, 2, 2 ECHO: 0, 3, 3 ECHO: 0, 4, 4 ECHO: 1, 0, 5 ECHO: 1, 1, 6 ECHO: 1, 2, 7 ECHO: 2, 0, 8 ECHO: 3, 0, 9 ECHO: 3, 1, 10 ECHO: 4, 0, 11 ECHO: 4, 1, 12 ECHO: 4, 2, 13 ECHO: 4, 3, 14 ECHO: 4, 4, 15 ECHO: 4, 5, 16
Open this post in threaded view
|

## Re: double for()

Open this post in threaded view
|