I can say it in Javascript, but not with the [dict] objects.

Charles Turner's icon

Greetings-

I've spent a while looking at this issue using the [dict] family of objects with not much luck. I have a stream of data coming in from a Bitwig OSC interface, and I'm trying to build a data structure out of some of it. I can build a Javascript object thusly:

var track = {};

function bang()
{
    track[1] = {};
    track[1].exists = 1;
    track[1][1] = {};
    track[1][1].content = 1;
    track[1][1].name = 'foo';
    outlet( 0, track[1].exists);
    outlet( 0, track[1][1].content);
    outlet( 0, track[1][1].name);
}

But get hit with some conundrums with [dict] and friends. Either I end up trying to convert a previously defined value to a key, or need to construct my Dict in the reverse temporal order of the data stream, which seem either not possible or not appetizing. Any thoughts? Thanks!

tyler mazaika's icon

It's hard to infer exactly which part of this is hanging you up. So I'll just mention a couple things that I've found I had to do a lot when working with Dicts…

1) I use 'replace' rather than 'set' messages most of the time. It helps to be able to make a call like "replace a::new::nested::datastructure <value(s)>" without being told that the dict "nested" doesn't exist. Might ease your "reverse temporal order" pain?

2) If you can build your datastructure in JSON, you can use JSON.stringify() and Dict.parse() to get a Dict object. Then you could just output the dictionary.name to use elsewhere in your Max code.

Cheers,
Tyler

Charles Turner's icon

Hi Tyler-

Thanks for your kind response. #2, yes, during lunch it occurred to me that I could build it in Javascript and then serialize it in a form suitable for a Dict.

As regards #1, I have a stream that looks like this:

trackexists: 1 1
clipname: 1 1 deadbeef
cliphascontent: 1 1 1
cliphascontent: 1 2 0
clipname: 1 3 foo
cliphascontent: 1 3 1

The lines read, in order of receipt, as:
track 1 exists 1
track 1 clip 1 name 'deadbeef'
track 1 clip 1 hasContent 1
and so forth...
I've approached this in quite a number of ways, and the below might not be the most successful, but it illustrates the issue with just the first line of the stream. track 1 should be the first element of the Track array--a value--but also the key for the exists 1 KV pair. There are exist entries for an entire forthcoming array of Tracks.

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

Thanks again for your interest! Charles

Charles Turner's icon

Since my last post, I do think I'm making progress here. Very helpful to simply type out a static JSON declaration of my envisioned data structure. There are differences between JS object construction and JSON that I've ignored...

tyler mazaika's icon

Oh what I would do to have a straightforward way to work with indexed array of dictionaries. 😭

Charles Turner's icon

Yes, it's kind of weird. I wrote out the JSON for my dictionary and felt I'd made some progress, but still the brick wall. I've spent more than a half day on this, and if I had just gone with my first iteration of a Javascript object (or even Jitter) I would have been done with the writing of constructors and accessors long ago. :-(

Charles Turner's icon

In case anyone's interested in the dict structure I'm trying to create:

{
    "track" : [
        { "exists" : 1 , "clip" : [{"name" : "deadbeef1", "content" : 1}, {"name" : "foo1", "content" : 1}, {"name" : "200504-1", "content" : 1}]},
        { "exists" : 1 , "clip" : [{"name" : "deadbeef2", "content" : 1}, {"name" : "foo2", "content" : 1}, {"name" : "200504-2", "content" : 1}]},
        { "exists" : 1 , "clip" : [{"name" : "deadbeef3", "content" : 1}, {"name" : "foo3", "content" : 0}, {"name" : "200504-3", "content" : 1}]},
        { "exists" : 0 , "clip" : [{"content" : 0}, {"content" : 0}, {"content" : 0}]}
    ]
}