Add multiple items to a flat list in a loop.

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

Add multiple items to a flat list in a loop.

dinther
This one is a beginners thing for sure but after 4 hours head scratching...
Consider this simple loop that produces 4 values per loop

p = concat([1],[for (i=[1:4:9]) [i+1,i+2,i+3,i+4]]);
echo(p);

ECHO: [1, [2, 3, 4, 5], [6, 7, 8, 9], [10, 11, 12, 13]]

However, what I want is a flat list like:

ECHO: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

I managed this weird contraption but that looses the "1" at the start

echo([for (i=[0:1:len(p)-1], j=[0:1:len(p[i])-1]) p[i][j]]);
ECHO: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

How do I do this?

Sent from the OpenSCAD mailing list archive at Nabble.com.

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

Re: Add multiple items to a flat list in a loop.

dinther
And of course I find a solution immediately after I posted.

function flatten(a)=
    [ for(x=a)
      for(b=len(x)==undef? x:flatten(x)
         )
      b ];

But if there is any info that helps me to manage my lists better, I love to hear.

Sent from the OpenSCAD mailing list archive at Nabble.com.

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

Re: Add multiple items to a flat list in a loop.

bassklampfe
In reply to this post by dinther
The keyword 'each' does the requested magic

p = concat([1],[for (i=[1:4:9]) each [i+1,i+2,i+3,i+4]]);
echo(p);

ECHO: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]



Am 09.03.21 um 06:09 schrieb dinther:
> This one is a beginners thing for sure but after 4 hours head
> scratching...
> Consider this simple loop that produces 4 values per loop
>
> p = concat([1],[for (i=[1:4:9]) [i+1,i+2,i+3,i+4]]);
> echo(p);
>
> ECHO: [1, [2, 3, 4, 5], [6, 7, 8, 9], [10, 11, 12, 13]]


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

Re: Add multiple items to a flat list in a loop.

tp3
On 09.03.21 06:47, [hidden email] wrote:
> p = concat([1],[for (i=[1:4:9]) each [i+1,i+2,i+3,i+4]]);

The concat() is not even needed anymore in this case:

p = [1, for (i=[1:4:9]) each [i+1,i+2,i+3,i+4]];

ciao,
  Torsten.

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
-- Torsten