Dict / Coll - Key/Index Value Ranges?

Christopher Purcell's icon

Hope you're all keeping happy and healthy.

I am using this time to try and learn Max. Slowly making progress thanks to resources like this forum, but I have hit an issue to which I cannot find an answer.

I have built a coll using numbers as the index value, and some text as the associated data for said indices. For the purpose of this example, imagine that these pairs are page numbers, and their corresponding chapter titles.

The coll contains a full list of every page number and chapter title in a book. For example:

1, "Moonlight and Bloodshed"
16, "Their Clothes Smelled of Roses"
26, "Left Luggage"
43, "The College Silver"

What I want to be able to do is have the coll output the associated chapter name when any value within the page range is input. For example:

15 = "Moonlight and Bloodshed"
32 = "Left Luggage"

However, I don't believe this to be possible by using coll, without somehow re-populating the coll so that all of the 'blanks' in between the index values are also filled.

Is is possible to define a range of 'keys' using dict instead? Any other workaround for storing/retrieving data associated to a range of values?

Thank you in advance.

pdelges's icon

If your coll entries are sorted, you could use the next message to query the coll until your index is bigger than your page number.
Not very efficient, but possible.
If you need something more efficient, then you could (automatically) create another coll with an entry for every page, ± like this :

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

tyler mazaika's icon

[funbuff] object could help. You could just populate it with the mapping of index -> index from the coll. If funbuff doesn’t have the index you request from it (e.g. 15) it will output its next lowest stored value (in your case 1). Then you could use that to lookup up the symbol from coll.

Christopher Purcell's icon

Excellent! Thank you both - I sat and worked through the references to understand all solutions presented; [funbuff] appears to give the most succinct answer to this particular question. I had thought about using [scale] to try and 'compress' the incoming values to the index stored in the coll, but couldn't think of a way to update the variables; [funbuff] overcomes that perfectly.

Thanks again for sharing your knowledge here.