'Is it possible to interleave two arrays/lists in Openscad
I have 2 arrays and I'd like to interleave their values.
For example
Interleave([[1,2],[2,2],[3,2]], [[3,1],[4,1],[5,1]]); // Should yield [[1,2],[3,1],[2,2],[4,1],[3,2],[5,1]]
However I can't get something like this to work.
function Interleave(Set1,Set2) =[for(x=[0:len(Set1)-1]) Set1[x], Set2[x]];
Solution 1:[1]
The comma is not doing what you expect. It's separating 2 list entries, the first one is the for generatator and the 2nd one is just Set2[x] which is undefined as the x belongs to the for.
You can see what's going on when using just a string for the second part:
function Interleave(Set1,Set2) =[for(x=[0:len(Set1)-1]) Set1[x], "test"];
// ECHO: [[1, 2], [2, 2], [3, 2], "test"]
To get the interleaved result, you can generate a list with 2 entries for each interation and unwrap that using each
function Interleave(Set1,Set2) =[for(x=[0:1:len(Set1)-1]) each [Set1[x], Set2[x]]];
Also note the ..:1:.. step size, this ensures sane behavior in case Set1 is empty. The start:end range behaves differently for backward compatibility reasons.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Torsten Paul |
