MXS Dictionary

0 votes
Author Name: 
Christoph Buelter

A very simple pure mxs implementation of a dictionary to store key-value pairs.

It is somewhat "fake" in that it is not using any hashing but an internal array of tuples. Which means the bigger it gets the slower it will be to work with. So its usage is probably quite limited. If you however desperately need a dictionary data type and can't use any fancy dotNet, this should work down to Max version 9.

Example usage:

-- Either copy/paste into your script or bundle the file with your main script file and use
fileIn "dict.ms"
-- or
include "dict.ms"
 
-- Available methods are shown in these examples:
 
d = dict()
d.add "Peter" 34
d.add "Zacharias" 29
d.add "Christian" 12
d.add "Hank" 56
 
d.items()
--#(#("Peter", 34), #("Zacharias", 29), #("Christian", 12), #("Hank", 56))
d.keys()
--#("Peter", "Zacharias", "Christian", "Hank")
d.values()
--#(34, 29, 12, 56)
 
a = d.sortByKey()
--#(#("Christian", 12), #("Hank", 56), #("Peter", 34), #("Zacharias", 29))
b = d.sortByValue()
--#(#("Christian", 12), #("Zacharias", 29), #("Peter", 34), #("Hank", 56))
 
d.add "George" "McDolan"
b = d.sortByValue()
--"SortException: Dict values must be of same comparable type to sort"
 
d.hasKey "Hank"
--true
d.get "Hank"
--56
 
i = d.pop "Hank"
--#("Hank", 56)
d.items()
--#(#("Peter", 34), #("Zacharias", 29), #("Christian", 12))

Cheers

Version Requirement: 
9