An array for menu items?

I send a command via a MIDI CC to KM and in turn it finds a menu on the screen (one of those not-reported menus, so this is the only way to get it to open), and schools down the list by (CC value) number of times. But there are now a whole lot of things in that menu. I’m wondering if I can have an array of strings that are the names it should search for depending on the CC - so if it received a value of 120 it would look at the one hundred-twentieth item in the array and search for that by name. Seems like that would be quicker, and sometimes the scrolling bumps lose count because it’s too many of them. Anyone?

KM has pseudo arrays for text -- you specify one or characters as the "item delimiter" and can then access the pseudo array by element.

The default delimiter is ,, so the string

bird,cat,dog

...can be treated as an array containing three elements. If that string is held in the variable Local_theText then

%Variable%Local_theText[2]%

...would be cat.

A 120 item comma-delimited list would be horrible to maintain, so you'd be better off putting each item on its own line and using the newline character as your delimiter. So Local_theText would be

bird
cat
dog

and

%Variable%Local_theText[2]\n%

...would be cat -- note the added \n above, indicating the text should be split on newlines.

Demo macro:

Array Demo.kmmacros (3.8 KB)

KM does have "true" arrays through JSON, but that's overkill for what you want.

2 Likes

That’s amazing, and thanks!

is there a limit on array size using this method?

That's doubtful, since it's not really an array anyway, it's just lines of text, and there's no limit on the number of lines of text you can have in a variable, as far as I know.

There might be a performance impact for very large text variables, but I've used variables containing megabytes of text and didn't notice any performance issues.

1 Like

One other question - if I use the incoming CC to select the line in the array, how can I take that text and virtually “type it in” while the menu is open? It seems like Insert Text by Typing would work if I entered this into the field: %Variable%Local_theText[my CC amount which sets a variable]%. It seems like it should work but it doesn’t.

I think I see two mistakes in your written example. First, you said you wanted to select a line in a variable, (please don't call it an array) but you didn't include the "\n" string in your token. If you go back to the example written by Nige, he had "\n" inside the token, and you don't. Second, you need a variable inside the brackets, but all you have are some English words which I don't understand. You should place your value into a variable before you use that token, and then you should name the variable inside the brackets.

I don't know much about MIDI, so I don't know what value you need or what variable you are trying to set. It would be extremely helpful if you showed your code.

The word array just means an ordering or sequence.

A text can be understood as an array of, for example, characters, words, lines, paragraphs or chapters – whichever level of segmentation happens to work for the task in hand.

Here we're treating a text as an array of lines.

1 Like

Based on my respect for you, I'm reconsidering my terminology. It's just not implemented as an array internally to the KM Engine, which is what I was thinking about. But maybe I should be thinking from the user's POV and not from the Architect's.

2 Likes

As @Airy said, you missed out the \n -- that means your text will be split on the default , and if there aren't any in the text it will be treated as an array of 1 item.

And don't forget that the [...] part is effectively a "number" field (even though it is within a text token). So if you are using a variable there you'd use the same format as in other number/calculation contexts and not use the %Variable% token:

%Variable%Local_theText[Local_ccNumber]%