Challenge: print the 3rd item in this list

Nov 28, 2011 at 10:22pm

Challenge: print the 3rd item in this list

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. –
#60319
Nov 29, 2011 at 6:09am

Hello,

i don’t think it is a good idea to use reserved words like “list” “float” “bang” “symbol” as messages in your flow ; you can parse and substitute them with regexp to avoid strange behaviors.

– Pasted Max Patch, click to expand. –
#217104
Nov 29, 2011 at 11:13pm

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. –
#217105
Nov 29, 2011 at 11:48pm

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

#217106
Nov 30, 2011 at 12:58am

@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
#217107
Nov 30, 2011 at 4:55pm

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

#217108

You must be logged in to reply to this topic.