Using dict.unpack with a variable number of key/value pairs

Dan Laüt's icon

Given a dict with three keys, each key heading a subdict containing 3 key/value pairs.A umenu sends the selected subdict to a subroutine to extract the values.

And this is where it is stuck.

The only approach I can see is to use a fixed-length dict.unpack[key_1: key_2: key_3:].
That would mean the subroutine won’t work on dicts with a different number of key/value pairs.

The keys attribute would seem the way out, but it is quite unclear how that should work.
They are set when the subdict comes in, but only the first one is output.

Searches for more detailed documentation on the subject have come up nought.

Max Patch
Copy patch and select New From Clipboard in Max.

Any advice would be most welcome!

Source Audio's icon
11OLSEN's icon

You need to give dict.unpack some default keys to initialize the number of outlets of the object.

Max Patch
Copy patch and select New From Clipboard in Max.

But if you expect dynamic number of key/value pairs, why not use another dict.iter for the subdicts?
It depends on what want to do with the unpacked values.

Dan Laüt's icon

Yes, that makes sense. Thanks!
But if you reason that through, you'd have to initialize it with all 256 outlets.

This is part of a project to set up a dict with my numerous tune sketches, that will show the musical parameters of each, as well as the score of the first few bars, and play those.
I have such a database in FileMaker, but FM stinks at playing music real time, so that's why.
I exported the FM records as csv and built a routine to convert to dict. (Not easy, with all them quote marks). Now I need to convert that dict to one containing the score and audio refs as well. Some of the fields in the subdict must be converted in the process, e.g. modus is now a symbol, while it should be an int (1 for major, 2 for minor and 0 for anything else, say). Of course, I could set it up customized for this particular dict, with 6 fields, but my programmer's heart longs to make it more generally applicable. Don't want to waste all that work on a one-time use.

Dan Laüt's icon

The Source Audio scheme works perfectly. Here's the patch.

Max Patch
Copy patch and select New From Clipboard in Max.