Search code examples
arraysloopspuredata

How to write to arrays using loops in PureData?


I'm writing random numbers in an array using loops in Pure Data. For some reason in loop one Pure Data starts writing from index 1 instead of 0 and finishes on index 0 after the loop, this is not what I want. In loop two I found a solution using an extra [f] and [bang], Pure Data does the expected, starting from index 0. In both cases console prints the data in the same order (numbers are random, the order is the same), just the graph, reflecting the array, is different.

Does anyone know why the first case fails and the second works? My guess is that its related to the flow of data into the left and right inlets of [tabwrite].

loop one

Code

Code

Console

Console

Graph (1.151 is on index 1 instead of index 0)

Graph (1.151 is on index 1 instead of index 0)

loop two

Code

Code

Console

Console

Graph (1.742 is correct on index 0)

Graph (1.742 is correct on index 0)


Solution

  • Message order. You have a fanning output on the [f] object. Use a trigger [t b f] (short for [trigger bang float]) to make the order explicit. If you see an atom with multiple connections on one output, that's a red flag! Always use trigger objects, because without them the order in which you made the connections will be the order in which the messages will be sent. That's not visible in the patch, so it should be avoided!

    Read more about message order in the documentation of Pd

    patch with trigger

    There is another issue in your patch. Consider this case: If NotasPor0it is 8 and the counter is at 6, then NotasPor0it is changed to 4, the select object [sel] will never trigger the reset function of the counter idiom and continue to count... To avoid this issue, you could use a 'larger as' [>] symbol, or solve it with modulo (%) as seen in the screenshot.