<?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[Numbers incremented in a loop coming out in the wrong order]]></title><description><![CDATA[<p>I'm trying to increment two sets of numbers in a loop. One set comes out in the correct order; the other comes out in the reverse order. How can I make this work correctly? I've attached a simplified patch that demonstrates the problem. It should print<br />
0 0<br />
1 1<br />
2 2<br />
3 3<br />
4 4<br />
Instead it prints<br />
0 4<br />
1 3<br />
2 2<br />
3 1<br />
4 0</p>
<p><a href="/uploads/files/1750694278179-loop_test.pd">loop_test.pd</a></p>
]]></description><link>http://forum.pdpatchrepo.info/topic/15638/numbers-incremented-in-a-loop-coming-out-in-the-wrong-order</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Jul 2026 17:56:06 GMT</lastBuildDate><atom:link href="http://forum.pdpatchrepo.info/topic/15638.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 23 Jun 2025 15:54:14 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Numbers incremented in a loop coming out in the wrong order on Mon, 23 Jun 2025 15:58:08 GMT]]></title><description><![CDATA[<p>I'm trying to increment two sets of numbers in a loop. One set comes out in the correct order; the other comes out in the reverse order. How can I make this work correctly? I've attached a simplified patch that demonstrates the problem. It should print<br />
0 0<br />
1 1<br />
2 2<br />
3 3<br />
4 4<br />
Instead it prints<br />
0 4<br />
1 3<br />
2 2<br />
3 1<br />
4 0</p>
<p><a href="/uploads/files/1750694278179-loop_test.pd">loop_test.pd</a></p>
]]></description><link>http://forum.pdpatchrepo.info/topic/15638/numbers-incremented-in-a-loop-coming-out-in-the-wrong-order</link><guid isPermaLink="true">http://forum.pdpatchrepo.info/topic/15638/numbers-incremented-in-a-loop-coming-out-in-the-wrong-order</guid><dc:creator><![CDATA[jamcultur]]></dc:creator><pubDate>Mon, 23 Jun 2025 15:58:08 GMT</pubDate></item><item><title><![CDATA[Reply to Numbers incremented in a loop coming out in the wrong order on Mon, 23 Jun 2025 19:57:32 GMT]]></title><description><![CDATA[<ol>
<li>Do not split the control flow without <code>[trigger]</code></li>
<li>Why don't you use <code>[until]</code> ?</li>
</ol>
<p><img src="/uploads/files/1750708591375-image.png" alt="image.png" class="img-responsive img-markdown" /></p>
]]></description><link>http://forum.pdpatchrepo.info/topic/15638/numbers-incremented-in-a-loop-coming-out-in-the-wrong-order/2</link><guid isPermaLink="true">http://forum.pdpatchrepo.info/topic/15638/numbers-incremented-in-a-loop-coming-out-in-the-wrong-order/2</guid><dc:creator><![CDATA[FFW]]></dc:creator><pubDate>Mon, 23 Jun 2025 19:57:32 GMT</pubDate></item><item><title><![CDATA[Reply to Numbers incremented in a loop coming out in the wrong order on Mon, 23 Jun 2025 20:30:57 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-a" href="http://forum.pdpatchrepo.info/user/ffw">@FFW</a> I had the control flow correct in my example. Adding [trigger] didn't fix the problem. Here's my example with [trigger]. The numbers incremented in the [i] object still come out in the wrong order. It appears that they are coming out in LIFO order when they should be coming out in FIFO order.</p>
<p><a href="/uploads/files/1750710425370-loop_test.pd">loop_test.pd</a></p>
]]></description><link>http://forum.pdpatchrepo.info/topic/15638/numbers-incremented-in-a-loop-coming-out-in-the-wrong-order/3</link><guid isPermaLink="true">http://forum.pdpatchrepo.info/topic/15638/numbers-incremented-in-a-loop-coming-out-in-the-wrong-order/3</guid><dc:creator><![CDATA[jamcultur]]></dc:creator><pubDate>Mon, 23 Jun 2025 20:30:57 GMT</pubDate></item><item><title><![CDATA[Reply to Numbers incremented in a loop coming out in the wrong order on Mon, 23 Jun 2025 21:25:15 GMT]]></title><description><![CDATA[<p>This is your triggerized patch:<br />
<img src="/uploads/files/1750713340425-image.png" alt="image.png" class="img-responsive img-markdown" /></p>
<p>You can see the right branch is triggered before the top-most <code>[t f f]</code> feeds the <code>[pack]</code> so the numbers are all generated by the loop before they was printed.<br />
EDIT: you stack computation branch and they are released deepest to shallowest so numbers are reverted.</p>
]]></description><link>http://forum.pdpatchrepo.info/topic/15638/numbers-incremented-in-a-loop-coming-out-in-the-wrong-order/4</link><guid isPermaLink="true">http://forum.pdpatchrepo.info/topic/15638/numbers-incremented-in-a-loop-coming-out-in-the-wrong-order/4</guid><dc:creator><![CDATA[FFW]]></dc:creator><pubDate>Mon, 23 Jun 2025 21:25:15 GMT</pubDate></item><item><title><![CDATA[Reply to Numbers incremented in a loop coming out in the wrong order on Mon, 23 Jun 2025 21:53:07 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-a" href="http://forum.pdpatchrepo.info/user/ffw">@FFW</a> said:</p>
<blockquote>
<p>EDIT: you stack computation branch and they are released deepest to shallowest so numbers are reverted.</p>
</blockquote>
<p>I don't understand your comment. Why aren't the numbers coming out in the order in which they were computed? This seems like a bug to me. Is this behavior documented?</p>
]]></description><link>http://forum.pdpatchrepo.info/topic/15638/numbers-incremented-in-a-loop-coming-out-in-the-wrong-order/5</link><guid isPermaLink="true">http://forum.pdpatchrepo.info/topic/15638/numbers-incremented-in-a-loop-coming-out-in-the-wrong-order/5</guid><dc:creator><![CDATA[jamcultur]]></dc:creator><pubDate>Mon, 23 Jun 2025 21:53:07 GMT</pubDate></item><item><title><![CDATA[Reply to Numbers incremented in a loop coming out in the wrong order on Tue, 24 Jun 2025 02:25:48 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-a" href="http://forum.pdpatchrepo.info/user/jamcultur">@jamcultur</a> It's not a bug -- it's normal behavior.</p>
<p>It's documented in the html manual: <a href="https://msp.ucsd.edu/Pd_documentation/resources/chapter2.htm#s2.4.2" rel="nofollow">https://msp.ucsd.edu/Pd_documentation/resources/chapter2.htm#s2.4.2</a></p>
<blockquote>
<p>2.4.2. Depth first message passing<br />
Whenever a message is triggered in Pd, the receiver may then send out further messages in turn, and the receivers of those messages can send yet others. So each message sets off a tree of subsequent messages. This tree is executed in depth first fashion.</p>
</blockquote>
<p>&quot;Depth first&quot; means that, when a value comes out of an object's outlet, that branch of the tree must continue all the way to its end before the same outlet can go down a different branch, or before the same object can send data out of a different outlet.</p>
<p>Take a simpler example:</p>
<p><img src="/uploads/files/1750731721590-pd-order.png" alt="pd-order.png" class="img-responsive img-markdown" /></p>
<p>When the random number goes down the <code>[* 2]</code> branch, it <em>must</em> continue down to the [print secondValueCalculated] <em>before</em> it can advance down the [print firstValueCalculated] branch. The <em>whole</em> <code>[* 2]</code> branch must 100% complete.</p>
<p>Humans might look at this patch and think, &quot;Well, the 'print first' branch is simpler, so, intuitively it should be done first.&quot; Computers don't think like that.</p>
<p>In code (I'll use SC), it would look like:</p>
<pre><code>(
~func1 = {
    var number = 10.rand;
    ~func2.value(number);
    &quot;first value = %\n&quot;.postf(number);
};

~func2 = { |number|
    var result = number * 2;
    &quot;second value = %\n&quot;.postf(result);
    result
};

~func1.value;
)

-&gt;

second value = 14
first value = 7
</code></pre>
<p>~func1 specifies to do ~func2 first, then print. The Pd patch is the same.</p>
<p>I wish Pd's documentation called more attention to looping formulas that Just Work. Users often end up just trying to figure it out for themselves and getting tied in knots.</p>
<p>A <code>for</code> loop goes like this: <code>for(i = 0; i &lt; count; i++) { loop body }</code></p>
<p><img src="/uploads/files/1750731878055-pd-for-loop.png" alt="pd-for-loop.png" class="img-responsive img-markdown" /></p>
<p>That is, in C, there's a standardized way to write a counting loop -- following the formula avoids confusion. In Pd, there does exist (what should be) a standardized way to write a counting loop, and just like in C, following the formula avoids confusion -- but the formula isn't well enough known, and tricky for users to discover.</p>
<p>hjh</p>
]]></description><link>http://forum.pdpatchrepo.info/topic/15638/numbers-incremented-in-a-loop-coming-out-in-the-wrong-order/6</link><guid isPermaLink="true">http://forum.pdpatchrepo.info/topic/15638/numbers-incremented-in-a-loop-coming-out-in-the-wrong-order/6</guid><dc:creator><![CDATA[ddw_music]]></dc:creator><pubDate>Tue, 24 Jun 2025 02:25:48 GMT</pubDate></item></channel></rss>