discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Add multiple items to a flat list in a loop.

D
dinther
Tue, Mar 9, 2021 5:09 AM

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: http://forum.openscad.org/

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: http://forum.openscad.org/
D
dinther
Tue, Mar 9, 2021 5:13 AM

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: http://forum.openscad.org/

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: http://forum.openscad.org/
JO
jjvb-openscad@bassklampfe.de
Tue, Mar 9, 2021 5:47 AM

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]]

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]]
TP
Torsten Paul
Tue, Mar 9, 2021 12:04 PM

On 09.03.21 06:47, jjvb-openscad@bassklampfe.de 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.

On 09.03.21 06:47, jjvb-openscad@bassklampfe.de 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.