<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Question about arrays.]]></title><description><![CDATA[<p dir="auto">What is the proper way to work with arrays that have been generated with a for loop and push?<br />
Excuse if my terminology is not correct. I'm fairly new to openjscad and js as a whole.</p>
<p dir="auto">In essence I have an array of positions and want to punch holes in a plate at those positions.</p>
<p dir="auto">I am trying with the following:</p>
<pre><code>function pcb () {
    pcb_width = 25;
    pcb_height = 3;
    pcb_thickness = 50;
    pcb_dimmensions = [(pcb_width), (pcb_thickness), (pcb_height)];
    
    return cube(pcb_dimmensions);
}

function holes1 () {
    var array = [];
    array.push(translate([10,10,0],
                    cylinder({h: 20})));
    array.push(translate([5,10,0],
                    cylinder({h: 20})));
    return array;
}

function holes2 () {
    var holes = [];
    var positions = [
        [4, 12, -1],
        [8, 12, -1],
        [8, 18, -1],
        [12, 18, -1],
        ];
    
    for (i=0; i &lt;= positions.length; i++) {
        holes.push(translate(positions[i],
                cylinder({h: 10})))
    }
    return holes
}

function main () {
    //return holes2 ();
    //why does return [pcb()] work but not return [holes1(), or holes2()] ?
    //return [pcb(),holes1()] //does not work
    //return difference (pcb(),holes1());      // does not works
    return union (pcb(),holes1());      //works
    //return union (pcb(),holes2());    //does not work
}
</code></pre>
<p dir="auto">Questions:</p>
<ol>
<li>Why can I not return an array consisting of the pcb and the holes-array (cylinders)? Is nesting a problem here?</li>
<li>Union of pcb() and holes1() works, but not if holes2() is used. The function which includes a for loop.</li>
<li>Difference of pcb() and holes() works for neither.</li>
</ol>
<p dir="auto">As a side note, I am assuming the community hangs here and not on IRC or such?<br />
Looking for a place to quickly solve some doubts to help me get on my feet.</p>
]]></description><link>https://openjscad.nodebb.com/topic/190/question-about-arrays</link><generator>RSS for Node</generator><lastBuildDate>Sun, 14 Jun 2026 07:18:08 GMT</lastBuildDate><atom:link href="https://openjscad.nodebb.com/topic/190.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 31 Oct 2020 23:42:00 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Question about arrays. on Mon, 02 Nov 2020 05:52:49 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jarshvor" aria-label="Profile: jarshvor">@<bdi>jarshvor</bdi></a> another way to look at this is the return types...</p>
<p dir="auto">pcb() returns an object (1)<br />
holes1() returns an array of objects (2)<br />
holes2() returns an array of objects (4)</p>
<p dir="auto">V1 <a href="http://OpenJSCAD.org" rel="nofollow ugc">OpenJSCAD.org</a> requires main() to return an ARRAY of objects or a single object.<br />
(FYI, V2 automatically flattens everything into an array.)</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/gilboonet" aria-label="Profile: gilboonet">@<bdi>gilboonet</bdi></a> is correct in the example provided. By using '...holes1()' syntax, an array is constructed from [ pcb(), holes1[0], holes1[1] ]</p>
<p dir="auto">Hang in there. You'll get it soon. <img src="https://openjscad.nodebb.com/assets/plugins/nodebb-plugin-emoji/emoji/android/1f642.png?v=552b9a4dc0c" class="not-responsive emoji emoji-android emoji--slightly_smiling_face" style="height:23px;width:auto;vertical-align:middle" title=":)" alt="🙂" /></p>
]]></description><link>https://openjscad.nodebb.com/post/513</link><guid isPermaLink="true">https://openjscad.nodebb.com/post/513</guid><dc:creator><![CDATA[z3dev]]></dc:creator><pubDate>Mon, 02 Nov 2020 05:52:49 GMT</pubDate></item><item><title><![CDATA[Reply to Question about arrays. on Sun, 01 Nov 2020 08:23:48 GMT]]></title><description><![CDATA[<p dir="auto">I recently had same trouble with arrays. If you want to return your values without being forced to union them, you can use ... on those who are iterable (mainly arrays).</p>
<p dir="auto">pcb() returns a value, so it is not iterable, you return it directly<br />
holes1() returns an array so you can return ...holes1()</p>
<p dir="auto">put together that makes</p>
<p dir="auto">return [pcb(), ...holes1()]</p>
]]></description><link>https://openjscad.nodebb.com/post/510</link><guid isPermaLink="true">https://openjscad.nodebb.com/post/510</guid><dc:creator><![CDATA[gilboonet]]></dc:creator><pubDate>Sun, 01 Nov 2020 08:23:48 GMT</pubDate></item></channel></rss>