Feature Request: Filter or Token: "nth Line"

@peternlewis I'd like to have either a Filter option, or a Token, to return the "nth" line from a multiline variable.

If you already have something like this, please let me know, however I did do a lot of searching before I posted this, because it seemed a fairly obvious thing.

But, assuming you don't already have this, I'm guessing not many need it or they would have already asked for it. Still, if you're bored sometime... :laughing:

Thanks.

1 Like

Unless I am misunderstanding what you are asking for, this already exists:

EXAMPLE Multi Line Variable.kmmacros (3.0 KB)
EXAMPLE Multi Line Variable

If you don't want to alter the original Variable you can make a new Local Variable to get the nth line from.

1 Like

Thanks. Doesn't that assume the list doesn't contain any lines with commas in them?

Hmmm, I wonder if there's a JSON way... (hunkers down to do some sleuthing...)

Yes, but I suppose I've never come across that situation in my Macros.

But if I did (and only because I am scared of Regex) I would probably do a series of Keyboard Maestro search are replaces one after the other:

First one to replace commas with some weird unused character like ★, second search to replace line returns with commas. Then I could get the ★ sign back to a comma when accessing the nth bit of data.

But I'm sure there is a more elegant Regex or JSON solution. Either way it's a trick I've used often - usually the other way around (the Variable is comma separated but I want to display it as multi-line).

Let me tell you my experience with regular expressions (which I will now abbreviate as "regex").

I'm retired now, but I started programming professionally in 1979. But even with all that experience, I stayed away from regex until, I don't remember exactly, but around 6 years ago.

I could slap myself for waiting so long.

I think just about everyone agrees that the key to learning regex is to try to use it as often as possible. So I had to make a commitment that I would try to figure out how to do things in regex as much as possible. And it worked.

So I encourage you to do he same. It's head-bangingly obtuse at the start, but it gets better.

I recommend starting here: Learn regex the easy way - GitHub. The lessons have links to examples at one of my favorite websites, regex101.com.

You can do it. Go on, you know you want to... :laughing:

4 Likes

:rofl: :rofl: :rofl:

1 Like

But you can use any character you like since you’re just creating a variable array with a custom delimiter. I usually use a § which is probably a character I’ve never seen under normal circumstances. You can then continue to use the KM method of array addressing with no need for regex at all.

I do agree though that a filter built-into KM would be much more convenient.

2 Likes

Amazing!!! I never knew that. And the syntax is to put the Custom Delimiter Character after the closing ]

image

So my example can be changed to (and doesn't worry about any commas in the lines):

EXAMPLE Multi Line Variable - with custom delimiter.kmmacros (3.2 KB)
EXAMPLE Multi Line Variable - with custom delimiter

1 Like

I'm going to be making a lot of use of this. Especially as sometimes it is more useful to have the
Variable "Array" delimited by | instead of commas.

one|two|three|four can work in a Custom User Prompt Dropdown List when one,two,three,four doesn't. By having the custom delimiter as | I won't have to keep converting the Variables from one form to another.

1 Like

That's what I get for skimming over someone's response (@tiffle in this case). Thanks @Zabobon for making me look twice. Holy crap is that useful! :astonished:

1 Like

You guys with your 3-action solutions. Lazy programmers do it in a single line of code, like this:

image

It's a matter of opinion whether a "line" of text includes the trailing \n, but in this case I assumed it does. If you want to exclude the \n, I'm sure you can modify this statement.

P.S. I have used this technique in macros before.

P.S. I should have posted the macro in this thread. But since I've deleted it, I'll retype the important line for everyone here:

((.*)\n){%Variable%MyIndex%}(.*\n)(.*)

4 Likes

I knew there had to be a way to do this with regular expressions! Using the index along with the newline metachar is brilliant!!

I would have posted my solution earlier, but I was asleep.

I wonder if I could get KM to send me a wake-up alarm when I'm asleep and someone on this website posts a problem. :slight_smile:

I think you already spend too much time here. (Kidding. I think.) I mean, I'm spending a lot of time here right now, but I'll be getting back to real life as soon as I finish up one more KM project. (Suuuuure...)

There is a todo item that is not done requesting this to work:

%Variable%Source[3]\n%

But that does not work, so what you need to do is simple:

Search and Replace Variable Source for \n with “KM,KM”

And then

%Variable%Source[3]KM,KM%

You can use whatever text you want in the replacement, as long as it can never appear in the source string.

4 Likes

Please correct me if I'm wrong, but in both of your examples you can't replace "3" by any sort of variable or calculation.

Actually you can. (Which is a very good thing!) :grinning:

2 Likes

Funnily enough that's what I tried first. It would be good if it did work as that would remove the need to search and replace.

Oh boy. Fire me. Wait, you can't. I'm sure I've done this hundreds of times before. I just had a mental block.

A few days ago I was explaining the difference between a calculation and a string to someone. I said I make this mistake once in a while. Today is such an example.

Yes. I think you would have to be paid for your work here, in order to fire you :rofl: