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