Appending to an array of dictionaries

Messinki's icon

Hello!

I'm using a dict object to store data about midi notes as an array of dictionaries:

{
    "Notes" : [         {
            "Pitch" : 38,
            "Velocity" : 77,
            "Start" : 0.0,
            "End" : 0.065021
        }
,         {
            "Pitch" : 42,
            "Velocity" : 100,
            "Start" : 0.330751,
            "End" : 0.068439
        }
,         {
            "Pitch" : 42,
            "Velocity" : 96,
            "Start" : 0.66747,
            "End" : 0.040532
        }
]
}

From following this very useful page about the dict object I found that I can append dictionaries to an array of dictionaries by sending the [dict] object two messages: first (append Notes *), which creates a new entry, and then (setparse Notes[5] Pitch: 46 Velocity: 127. Start: 0. End: 1).

This works, but I have to know the length of the current array before I append any new value. Does anyone know if there is a way to append a dictionary to the end of an array of dictionaries without having to know what index to set it at? If anyone knows how to do this with Max or Javascript, that would be amazing!

Let me know if you want me to explain more/better...

Bradley Korth's icon

Well, if you were simply using a standard JavaScript array instead of a dictionary, then you could just use Array.prototype.push. In fact, you could make a JavaScript array of dictionaries.

soundyi's icon

I found a "pure Max" soltuion of doing this in a Cycling 74 example that was published in an article about what's new in Live 11 : https://cycling74.com/articles/what's-new-in-live-11-part-2

Download the patches and look at the subpatcher Add in the M4L device "Notes API - Before and After".

The solution is a specific "symbol syntax" with which you can add an dictionary easily to an existing array inside a dictionary, or by the means of an "append" message, one could also create an array out of former single instance and hence e.g. build an array of notes dictionaries.

I condensed the logic in the appended patch, which also shows this append technique - hope that helps.

By the way : using JavaScript is a nice solution, but leads to the consequence, that this code will be executed on the main thread (low priority queue) and if the you are doing this kind of dictionary stuff in a patch coming from a bang of a metro e.g., this part of your code will swap from the scheduler thread (high priority queue) to the main thread and if there is another branch of cords along the way and both are fused together down the "cords road" one may get some surprinsing results ... this threading thing can get nasty - heavy distortion alike ;-).

See : https://docs.cycling74.com/max8/vignettes/jsthreading

CreateArrayOfDictionariesInsideADictionary.maxpat
Max Patch
append to an array of dictionaries inside a dictionary - in pure Max

Russell Foltz-Smith's icon

@soundy1 thanks for this patch example.

what a crazy thing that it's that weird to just append to the end of an array. this breaks my brain. oh well, i guess this is why i like messing with max. it's just weird.