Confusion around accessing arrays as dict values

Dave's icon

Hi all, I'm very new to max and am having a bit of trouble getting my head around how to easily use arrays / dicts, and am wondering if someone could point me in the right direction.

Lets say I've got a dictionary with the following structure:

{

"60" : [ "foo", "bar" ],

"62" : [ "foo", "bar" ]

}


And I want to access the value stored at a given key (in this case the key is variable). So I do that by sending a message with the key I want to look up into [sprintf get %d], and then wire that into my dict input.

Connecting a print to the second output of my dict, I get something like:

print: 62 array u016000977

Now what I'm wanting to get is the reference to the actual array out of the dict's second output, so that I can then do array.tolist and look at the individual values within the list. But for the life of me, I cannot figure out a way of getting from this output to the actual array.

For instance, taking the dict's second output and doing [zl.nth 2] gives me the (string?) value "array", and doing [zl.nth 3] gives me the (also string?) value "u016000977". I have tried to array.tolist both of these values and end up with nothing in the case of "array" and "u016000977" in the case of "u016000977", and I'm also quite confused as to why they behave differently since they appear to my uninitiated mind as just "strings".

I've noticed that adding @legacy 1 to the dicts outputs my values as a list, which is where I want to be eventually anyway, but I would like to figure out how I'm intended to do that with the current API.

Please see below a screenshot showing my confusion. How can I get my foo bar? Any help much appreciated!





TFL's icon

Two options here:

  • add @legacy 1 to your [dict] so that arrays get output as lists directly

  • or add a [zl.slice 1] after the dict's second outlet, and connect [array.tolist] to [zl.slice 1] rightmost outlet. The message you get from dict is "62" array u123456789 so you need to remove that "62" part so that [array.tolist] understands it as an actual array and not as a list which gets converted to an array and back to a list (what is happening in your screenshot).

Dave's icon

That works perfectly, thank you!