Array with 'Return' as the delimiter isn't working?

Not sure if this is a bug or not, but I can't get the 'Return' token to work when trying to use a new-line-delimited variable as an array.

I've tried with 'set variable to text':

%Variable%myFakeArray[1]Return% returns the entire variable (not just the first line).
%Variable%myFakeArray[2]Return% returns an empty variable.
%Variable%myFakeArray[1]%Return%% returns the full variable, with "Return%" tacked onto the end.

With "Set variable to calculation":

myFakeArray[1]Return, the 'Return' gets colored red (as does, apparently, any delimiter item after the closing bracket.

Maybe I'm just doing it all wrong but I'm unclear as to what exactly I'm doing wrong...

This is on Mac OSX Sierra, Keyboard Maestro 9.0

Not sure if I understood correctly, but this works for me:

47-pty-fs8
[demo] Newline-separated Text into an Array.kmmacros (3.5 KB)

Result:

49-pty-fs8

There may be an easier ways to do this, not sure. I’m not using arrays that much.


Note on the regex:

\R matches any newline character and also a sequence of those (like CRLF).

http://userguide.icu-project.org/strings/regexp#TOC-Regular-Expression-Metacharacters


Instead of the comma you can also use a custom delimiter of your choice. But I wouldn’t use a newline character (even if the tokens would work) because there are many kinds of them: Return, Line Feed, CRLF, …

Several of my macros use a similar technique to replace newline characters with other delimiters. It works very well for preparing text for easier search and replace within KM. I never use a comma as a delimiter, though, because a comma can easily be part of one or more of my entries. Even if you don’t anticipate a comma being part of your text, it’s a good idea (in my opinion) to use a multi-character delimiter that has effectively no chance of being in the text (e.g., ::: or &&&). These also have the advantage that they are easy to spot in the converted list.

Also, keep in mind that you can reverse the process after changes are made, and replace the custom delimiters with newline characters if you’d like to keep the original format intact (but with the changes that the macro made).

As mentioned, you can use (almost) any custom delimiter you want.

I agree, this can be useful.

For this to work you first have to know/detect which newline character was used in the source. (LF, CR, CRLF)

I agree, but in my case, I make the lists to begin with, so I do know.

That’s good.

So, to return to your initial question about the %Return% token — and if you don’t want to replace it by some other char (or by a sequence of characters, in case this works) — I don’t know why the %Return% token isn’t accepted as delimiter. Might be a limitation of the variable notation syntax, or a bug. (@peternlewis?)

I couldn’t make it work either (But I didn’t spent too much time on it.)

That wasn’t I who asked the original question, but is the problem with the token as a delimiter perhaps that the % confuses things?

Oh, sorry, missed that. Sorry.

Yes, maybe. That’s what I meant with “limitation of the variable notation syntax”. Excuse my poor English. Only Peter knows.

That also brings up the question of whether array delimiter notation recognizes regex. For example, would %Variable%MyArray[3]\n% make any sense to KM?

\n is not necessarily a regex expression. It is a Line Feed. It works also in KM text fields (for example user prompts and others):

54-pty-fs8

08-pty-fs8

And no, \n doesn’t work as an array delimiter, at least not in my tests on my setup.

I’m pretty sure that regexes in general are not accepted as delimiter (they would have to be interpreted first).

I believe you can only use printable characters as the delimiter.
I could NOT find this so stated in the KM Wiki, so @peternlewis will have to confirm/correct.

Yeah only using printable characters as the delimiter would make sense! Not sure how I got it in my head that Return would work. I ended up using one of Tom's suggestions of just using Search and Replace, which works quite nicely! I save the altered one as a new array as I still need the original with new lines for some For Each actions - though maybe I could transition to Substrings instead?

You cannot generally have tokens within tokens (about the only exception to this is within nested brackets, such as: %Calculate%1+CHARACTERS(%LongDate%)%).

So the text between the ] and % is plain text. Even \n will not work.

As suggested, the technique to use is to replace the return with something that is plain text and unique (so generally not a comma). For example ",KMSEP,".

Search and replace \n with ,KMSEP, and then use [index],KMSEP,% at the end of your token.

1 Like

That's too verbose for my liking. I've often used a chevron «, which is OPT-\
Easy to type, and highly unlikely to occur in most text.

Anything that is plain text and unique is fine. Verbosity is hardly an issue since it only appears twice and you should never see it within the structure anyway.

Verbosity, like beauty and clutter, is in the eye of the beholder. :wink:

Sure, but you are going to get poked in the eye if you ever process text with french quotes in them…

I have no idea what you are taking about, but it is irrelevant at this point. This discussion has become verbose, so I'll stop here. :wink:

1 Like

I believe it's mainly because applescript uses french quotes - «» - to denote raw apple event code, which could conceivably break some stuff when you try and process it with applescript (my understanding is that a good bit of KM's back-end involves applescript).

1 Like

I doubt this because I use the « symbol all the time in my AppleScript code in Script Debugger 7. SD7 uses it as a marker for a particular comment line.

I just used it in a macro and it works fine.

image