Remove value from dict key?
Hey everyone,
I'm pretty new to Max, so please bare with me.
Is there a way to completely remove the value from a dict key while keeping the key?
I only found the messages "remove" (which also removes the key) and set.
When I try "set key_name" without any value, the key shows the value "" . Since I'm filling up an array with MIDI SysEx messages ("append key_name $1") the array would always start with "". Is there a way to completely empty the value?
Thanks a lot!
remove the key to wipe it togeher with pevious data,
reuse the key to append new sysex string if you need same key again.
why is that problematic ?
For instance when loading a new sysex string triggers a "remove" (to erase the previous stored string), but the key was removed manually already (by clear or whatever), than this results in an error message.
Of course I can work around that. I was just surprised that set key_name produces key_name : "" and that I couldn't find a way to completely empty the value without erasing the key.
dict is behaving so, if you have a key than it will have either some data or not, in which case "" = empty is added to a key.
you must have means of recognising sysex message as such,
an than want to store specific message under a specific key in dict.
Is that correct ?
once message is received and assigned to a key, receiving it again should wipe old one and insert it new.
What is your difficulty to achieve that ?
set message replaces old key AND data or insert it new if it does not exist. To do so you need to collect sysex message into a list,
instead of appending byte by byte.
Maybe you have difficulties with logic arround messages to dict,
to get any help with that you need to post the patch.
I understood that I can either use append (byte by byte) or set (collecting entire message in a list). Currently I'm using append, but I'll explore if there are any benefits using set instead.
As said: Right now the only problem with my patch are error messages when the "Load" action tries to wipe a key that has been deleted manually already, but that is an easy fix.
I just thought I'd miss something and tried to understand the mechanics of dict a little better.
Thanks again for your help, I really appreciate it!
[dict] has a @quiet attribute you can use to stop the error messages (such as from "remove" on a non-extant key).
If you wanted, you could also use "contains <key>" message to test whether a key exists. If it returns 0 (out third outlet), then you'd use a "set", if it returns 1 then you'd use an "append".
I saw @quiet, but I want to get better at Max and this feels a bit like cheating. ;-)
"Contains" does the trick. I use the value it returns with a gate to filter the "remove" messages: no more errors.
Thanks a lot!
In this case I’d bet the easy “cheating“ method probably yields better performance and code that’s easier to maintain. But I do understand the feeling about wanting to program around “error” messages.
Thanks, yes, you're certainly right because I just added additional objects which wouldn't be neccessary when just using @quiet instead. But performance speed is not critical at all for this patch – and my main reason for digging deeper than I have to (sometimes) is to learn something new, which I did here. So, mission accomplished. :-)