Challenge: print the 3rd item in this list

skot's icon

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]

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

Tricky, right?

skot's icon

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.

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

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

Luke Hall's icon

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"]

Christopher Dobrian's icon

@skot Your approach is clever and works.

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

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.

2999.mynth.js
js
skot's icon

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