Forums > MaxMSP

Challenge: print the 3rd item in this list

November 28, 2011 | 10:22 pm

Challenge: print the third item in this list:

(1 2 list 4 5)

ie, print "list".

This, for example, does not work:

(1 2 list 4 5) -> [zl nth 3] -> [print]

Tricky, right?

– Pasted Max Patch, click to expand. –

November 29, 2011 | 11:13 pm

I agree, those words create a problem, but what if you need to move text around and you can’t guarantee that the text won’t be one of those words?

Manipulating text in Max is tricky. You have to constantly stay viligant to keep it from inadvertently becoming a message.

I have data coming in as a list: 2 integers and a string. If I want to [print] that string (or append it to a [text] object, or do *anything* with it) I need a reliable way to pull it out of there that won’t fail if the string happens to be "list".

What’s needed it something almost like [zl nth] that outputs not the raw element but a 1-element list that *contains* the element, ie it sends (list ) rather than ().

I’ve managed to come up with just such an object, [nth], that works consistently if any, all, or none of the terms in the list are "list".

It accomplishes this by
1) prepending "fence" to the beginning of the list (in case the first element of the list is "list")
2) chopping off the end of the list after the nth element (well, n+1th now because of the fence from step 1)
3) adding "list" and "fence" at the end of the truncated list
4) reversing the list and truncating it to 3 elements — list is now (fence list )
5) slicing off the fence

If anyone can think of an easier way, I’d love to hear it.

Here’s [nth] plus a patch that demonstrates it:

– Pasted Max Patch, click to expand. –
– Pasted Max Patch, click to expand. –

November 29, 2011 | 11:48 pm

Those words are reseved in max for handling data types, if you want to use them you must convert them to symbols, here’s one brute force method using [regexp] which will mean you can [print] and play with them as long as you realise they’re wrapped in "quotes" from now on!

[regexp (list|int|float|matrix|bang) @substitute "%1"]


November 30, 2011 | 12:58 am

@skot Your approach is clever and works.

For handling strings in general, JavaScript is usually very handy. The attached "mynth" script puts the word "list" in quotes before sending it out, but leaves other things alone.

– Pasted Max Patch, click to expand. –
Attachments:
  1. mynth.js

November 30, 2011 | 4:55 pm

Thanks guys.

I’d add some "\b"s to the regexp to prevent output like b"list"er…

[regexp \b(list|int|float|matrix|bang)\b "%1"]

Or add int, float, matrix and bang to the list of words to quote in mynth.js.

However, both these solutions assume I don’t care about the difference between (list) and ("list"). But in real-world text, quotes make a difference:

CANDIDATE CAUGHT IN LOVE NEST WITH SINGER
CANDIDATE CAUGHT IN LOVE NEST WITH "SINGER"

If I’m processing the text of Hamlet, I don’t want to end up with

GHOST
List, "list", O "list"!

The best way I’ve found to treat a symbol as a string, without actually altering it, is to make sure it’s always an element of a list:

(list list) -> [print] => list


Viewing 5 posts - 1 through 5 (of 5 total)